Gareth Davies' fifth try of the tournament and the boot of Dan Biggar gave Wales the slenderest of leads at half-time despite Handre Pollard's impeccable kicking in response.
In a game that was a fantastic advert for knockout rugby from start to finish, the lead changed hands several times after the break as Pollard and Biggar duelled from the tee.
The Welsh fly-half is legitimately in the conversation as the best No.10 in the world but it was the combination of two world-class Springbok players - No.8 Duane Vermeulen and scrum-half du Preez - that settled the contest with six minutes remaining and booked a last-four clash with either France or New Zealand.
Wales spurned an early opportunity at Twickenham as three-time Lion Gethin Jenkins looped a pass over Tyler Morgan when the centre had the try-line at his mercy.
The red wave of attack continued but a stubborn South Africa defence held firm and in fact got on the board first when Pollard slotted a penalty after a transgression by George North.
Pollard slotted two more three-pointers as the Springboks exerted pressure at the scrum although Biggar's penalty in between kept Wales in touch.
And their attacking intent was rewarded with a 17th-minute try - Biggar brilliantly collecting his own up-and-under before feeding Davies to race under the posts.
However, yet more Welsh indiscipline enabled Pollard to go four-from-four soon after as the Springboks retook the lead.
North looked a constant threat with ball in hand - the winger gaining 53 metres in the first half - but it was the boot of Biggar that gave Wales a 13-12 half-time lead as he slotted a drop goal with the last kick of the half.
With 84 per cent of World Cup knockout games being won by the team leading at the interval, the omens looked good for Warren Gatland's troops and the pressure of being behind looked to tell on Pollard as he missed his first penalty early in the second half.
Biggar made him pay with a successful three-pointer after Schalk Burger was pinged but the South African kicker momentarily redeemed himself by nailing a 51st-minute drop goal.
However, he promptly missed second penalty - screwing his effort wide left - and while the Springbok back-row was looking ferocious in attack, their Welsh counterparts were refusing to yield a single yard in defence.
The consistent pounding away of the southern hemisphere side did pay dividends as Pollard slotted another penalty on the hour mark but Biggar responded in kind soon after to give his side a 19-18 advantage.
As the clock ticked down, the tension rose at Rugby HQ but it was a moment of magic from Vermeulen that helped settle proceedings with six minutes left.
The No.8 drew in Alex Cuthbert before flicking a sumptuous pass out of the back of his hand which enabled du Preez to dive over.
And the Springboks saw out the final few minutes in relative comfort as they now prepare for France or reigning champions New Zealand in what promises to be a mouth-watering semi-final.