The Springboks will face England in next Saturday's final following fly-half Handre Pollard's match-winning penalty four minutes from time.
Pollard kicked 14 points and centre Damian de Allende scored a try, with their opponents replying through wing Josh Adams' Wales record-equalling sixth try in one World Cup, three Dan Biggar penalties and a Leigh Halfpenny conversion.
Here, the PA news agency takes a look back at what was Wales coach Warren Gatland's penultimate match in charge.
Springboks edge hard-fought contest
It will not go down as a classic, but South Africa's players and coaching staff will not care after they came out on top in a kicking-dominated battle in Yokohama.
The first half was a war of attrition which ended with South Africa 9-6 ahead thanks to three Pollard penalties and, although Biggar kicked Wales level early in the second half, Damian de Allende burst through the Welsh defensive line to put South Africa 16-9 ahead.
Wales still had time to get back into the contest but captain Alun Wyn Jones boldly opted for a scrum rather than a penalty in front of the posts and the gamble paid off as the ball was worked wide for Josh Adams to dive over in the corner for a converted try that made it 16-16.
Rhys Patchell was off target with a long-range drop-goal attempt and the match was eventually decided in the 76th minute as Pollard held his nerve to slot the winning penalty.
What went wrong for Wales?
Wales coach Warren Gatland unsurprisingly acknowledged that his side paid the price for conceding too many penalties, with those that were in range of the posts all successfully kicked by Pollard.
"We probably gave away too many penalties in our own half and that cost us dearly but I'm proud of the fact that we never gave up and got ourselves back into that game and we were in the arm wrestle," Gatland said.
"Two or three scrum penalties, a couple of line-out penalties are disappointing and that's the difference between the two teams.
"They gave 100 per cent. It was a tough, physical South Africa team that we played against. Our guys didn't take a backward step and I can only be proud of them for that."
Josh Adams may have ended up on the losing side but his sixth try of the World Cup makes him the leading scorer, one ahead of Japan's Kotaro Matsushima and South Africa's Makazole Mapimpi.
What happens next?
For Wales it is an anti-climactic third/fourth place play-off against New Zealand on Friday, a match which marks the end of the long coaching reigns of Gatland and his All Blacks counterpart Steve Hansen. For South Africa it is on to Saturday's final against England, a repeat of the 2007 final in France won 15-6 by the Springboks.