South Africa (7) 30
Tonga gave South Africa a big scare in Lens
Tries: Pienaar (2), Smith, Skinstad
Cons: Pretorius, Montgomery
Pens: Steyn, Montgomery
Tonga (3) 25
Tries: Pulu, Hufanga, Vaki
Pens: Hola (2)
Cons: Hola (2)
South Africa were made to bring on the cavalry to muscle past a fired-up Tonga in the World Cup Pool A game in Lens.
The group leaders made 11 changes for the match but trailed 7-10 in the second half before calling up five first-choicers to mount a renaissance.
The Springboks surged 27-10 clear but brave Tonga fought back with two quick tries to set up a thrilling finish.
South Africa clung to their lead in a desperate last 10 minutes but the bonus-point win sealed the group.
The Springboks, who have yet to face the USA in their final pool match, will probably face Wales in Marseille on 7 October in their quarter-final.
Tonga's last game is against England in Paris on 28 September in what will be a winner-takes-all clash for the second qualification place from Pool A if England beat Samoa in Nantes on Saturday.
Tonga edged ahead with a Pierre Hola penalty on three minutes and were delighted to remain clear when South Africa's Andre Pretorius missed two shots at goal.
The abrasive Tongans had the Springboks rattled and it took 17 minutes before South Africa managed to cross the line, when Ruan Pienaar took a quick tap on the left and dived over.
Tonga were unfazed and an incisive break from open-side flanker and skipper Nili Latu set up a promising ruck in front of the Springbok posts.
Fly-half Hola rifled a long pass out to the left flank where the waiting prop Soane Tonga'uiha crashed over, only to be pulled back for a forward pass.
South Africa were unable to shake Tonga and they failed to stretch their lead when the unhappy Pretorius missed his third kick on 28 minutes.
The Boks then wasted three golden chances around the half hour mark to forge ahead.
Stout defending kept out winger JP Pietersen before a woeful pass from Pretorius squandered the second opportunity, and a spilled ball when diving for the line from Ashwin Willemse ruined the third.
It was a let-off for Tonga and they missed a penalty of their own on 37 minutes before Hola miscued a drop-goal attempt straight after the break.
But two minutes later Tongan prop Kisi Pulu muscled over from close range, shoved on by number eight Finau Maka, to give his side a shock lead.
Hola added the conversion and shortly after, South Africa failed to narrow the gap when Pretorius missed his fourth penalty.
Springbok coach Jake White sensed the trouble his side were in and drafted in Bryan Habana, Francois Steyn, Victor Matfield, BJ Botha and John Smit to bolster his side.
Steyn took over the kicking duties and dragged the Boks back level at 10-10 on 52 minutes, seemingly the tipping point in the match.
Replacement flanker Juan Smith, on for Danie Roussouw who was stretchered off, crashed over on 58 minutes before the resurgent Boks added quick-fire tries from skipper Bobby Skinstad and Pienaar.
Both sides had lost a man to the sin-bin for fighting in midfield - Joseph Vaka for Tonga and Steyn for South Africa.
But Tonga pounced when the Springboks also lost Habana to the sin-bin for handling on the floor, with Sukanaivalu Hufanga catching a cross-field kick to touch down on 71 minutes.
Tonga sensed a famous upset and a marauding length-of-the-field effort involving several hacks-on ended with Viliami Vaki scoring to claw his side back to within five points.
Springbok replacement Percy Montgomery, winning a record 90th cap for his country, nudged his side clear with a late penalty but Hola cancelled it out with two minutes left on the clock to leave the result in doubt until the final whistle.
South Africa: Pienaar, Willemse, Olivier, Julies, Pietersen, Pretorius, Januarie, Skinstad, Steenkamp, G. Botha, van der Linde, B. Botha, van den Berg, van Heerden, Rossouw.
Replacements: Smit, B. Botha, Matfield, Smith, Habana, Steyn, Montgomery.
Tonga: Lilo, Tu'ifua, Hufanga, Taione, Vaka, Hola, Tuipulotu, Maka, S. Tonga'uiha, Lutui, Pulu, Hehea, Kauhenga, Vaki, Latu.
Replacements: Taukafa, Toke, Afeaki, Filipine, Havea, Tupou, Havili.
Referee: W Barnes (RFU)