(CNN) -- Reigning champions South Africa are through to the Rugby World Cup quarterfinals after a hard-earned 13-5 win over Samoa on Friday.
The Springboks guaranteed themselves top spot in Pool D with a fourth-successive win at the New Zealand tournament, although Peter de Villier's team withstood a second-half fightback from Pacific islanders Samoa.
Wing Bryan Habana's 40th Test try, and second of the competition, after eight minutes proved enough to secure qualification for the two-time World Cup winners, and all but end Samoa's campaign.
Samoa's misery was complete in the closing stages, when New Zealand-born fullback Paul Williams was given a red card, the first of the tournament, for striking flanker Heinrich Brussow.
It looked as if the match would be plain sailing for South Africa, when Habana, 28, crashed over the Samoa line for a superb try before flyhalf Morne Steyn successfully converted.
All of South Africa's points were scored within the first 26 minutes, with Steyn adding a penalty before his namesake Frans did the same to make the score 13-0.
But Samoa rallied in the second half and got their reward with just under half an hour left to play, number eight George Stower going over in the corner to put them back in the contest.
Flyhalf Tusi Pisiata missed the conversion and Samoa were unable to score any further points, although they gave South Africa an almighty scare when center Eliota Fuimaono-Sapolu was penalized for a knock on inches short of the try line.
The game looked safe for South Africa when Williams received his marching orders, but the Boks replacement hooker John Smit was given a yellow card with 10 minutes left and ordered off the field.
South Africa held on, and will now face a last eight clash against the runners-up in Pool C, one of Ireland, Australia or Italy, on October 9.
Samoa's defeat means they can only reach the quarterfinals if Fiji beat Wales, currently second in the standings, by 39 points or more, scoring more than four tries in the process, in the final Pool D contest on Sunday.