The West Indies were 146 for eight at the close, an overall lead of 359.
West Indian captain Chris Gayle decided not to enforce the follow on after the tourists took a first innings lead of 213. Dwayne Bravo took four for 24 as South Africa were bowled out for 195.
At 122 for two in their second innings, the West Indies were heading for an indomitable position but they lost six wickets for 22 runs to give South Africa hope of winning a Test in which they were outplayed for most of the first three days.
The collapse was started by a direct-hit run-out by Herschelle Gibbs to dismiss Daren Ganga for 45. Fast bowler Dale Steyn took three wickets and left-arm spinner Paul Harris two as the momentum shifted dramatically in the last hour of play.
Gayle got the second innings off to a blazing start before he was caught behind off Makhaya Ntini for 29 off 22 balls.
Although Runako Morton was out cheaply, Ganga and Samuels put on 65 for the third wicket to put their side in a dominant position.
Ganga and Samuels were out in successive overs but they had ensured that South Africa would be set a target bigger than the best they have previously achieved in the fourth innings of a Test match - 340 against Australia in 2001/02.
Samuels followed Ganga when he dragged an attempted pull against Steyn into his stumps and wickets started to tumble with Steyn finally showing the form that brought him 20 wickets in two Tests against New Zealand last month.
Shivnarine Chanderpaul's run of form came to an end when he edged Steyn low to Jacques Kallis at second slip and Denesh Ramdin gloved a bouncer to gully.
Bravo was given a torrid time by Steyn but fell to Harris when he drove the spinner to mid-on. Harris also trapped Darren Sammy leg before.
Bravo, who finished with four for 24, took the last four wickets in 21 runs at a personal cost of six runs as the West Indies bowled out South Africa for 195.
South Africa suffered an early blow after resuming on 122 for five when Mark Boucher was out in the third over of the morning. Boucher top-edged a hook against Jerome Taylor and was caught by Daren Powell at fine leg.
But AB de Villiers and Paul Harris defied the West Indian bowlers for most of the morning in a seventh wicket stand of 43 before Bravo struck shortly before lunch.
De Villiers, the only top order batsman to shine, made a patient 59 off 127 balls before he was bowled by a superb delivery from Bravo which straightened off the pitch and clipped his off stump.
Bravo followed up with the wicket of Harris, who batted for 100 minutes and faced 68 balls in making nine. He was caught at mid-off when he attempted a forcing shot off the back foot.
South Africa's last two wickets fell in 13 balls after lunch as Bravo had Andre Nel caught at mid-on and Dale Steyn caught at fine leg.