© Getty Images
However, discipline cost Warren Gatland's side late in the first half as Luke Charteris and Dan Biggar were both sent to the sin bin, allowing the hosts to reduce the gap to three points by half time.
Wales opened their lead up again early in the second half when Ken Owens forced his way over just five minutes after the restart, while Biggar added two penalties to leave the team 13 points ahead going into the final 10 minutes.
However, Willie le Roux got South Africa back into contention as he found a gap in the 73rd minute, before they were awarded their second penalty try of the match for an illegal tackle.
Biggar tried to snatch it in the final seconds with a drop-goal attempt, but his kick came up just short.