Wales led going into the final 10 minutes but finally succumbed after enduring a second-half battering.
Fly-half Handre Pollard kicked five penalties and a drop-goal for the Springboks.
Wales' Dan Biggar scored 14 points and created the chance for Gareth Davies to score his fifth try of the tournament.
Veteran scrum-half Du Preez had to be persuaded to play in the tournament by South Africa coach Heyneke Meyer after a series of career-threatening injuries, but repaid the faith with a moment of coolness in a tumultuous atmosphere.
South Africa now face New Zealand in the last four after the world champions annihilated France 62-13 in Saturday's second quarter-final.
The Springboks pack gave a powerhouse display in the second half, but were met by extraordinary Welsh defence with captain Sam Warburton giving an outstanding display.
But a clever move from a scrum in the Wales 22 saw number eight Duane Vermeulen feed Du Preez, who exploited a huge gap on the blind-side to dive over in the corner.
Another veteran, flanker Schalk Burger, was outstanding as the Springboks turned the screw.
There were shades of Wales' preceding tournament defeat by Australia in the opening minutes as George North was hauled down short of the line. Then Wales blew an overlap when Gethin Jenkins' lobbed pass went over centre Tyler Morgan's head and into touch.
Although on top, the Welsh were behind as Wayne Barnes punished silly errors and the impeccable Pollard was unerring with his boot.
The battle of the breakdown saw skirmishes won by Sam Warburton and Gethin Jenkins for Wales and Duane Vermeulen and Bryan Habana for South Africa.
Wales' 13-12 lead at the interval was thanks to a full-blooded forward effort and the brilliance of Biggar, whose drop-goal edged them ahead after his chip-and-gather had set up Davies' try.
A chaotic start to the second half saw Biggar nail a long-range penalty, before South Africa - with Vermeulen, Burger, Eben Etzebeth and Lood de Jager in the vanguard - stepped up a gear.
Pollard's drop-goal got the Boks back to within a point, but he missed two penalties to mar an otherwise excellent display.
Wales were hanging on as Willie le Roux sliced through, but for all their dominance, the Springboks only reclaimed the lead when Pollard rediscovered his range with a penalty to edge his side ahead 18-16.
Biggar put Wales back ahead at 19-18, but with Gatland's men making more than twice as many tackles as their opponents, it was too much for a team who had survived the group of death to hang on in the face of waves of attacks.
South Africa flanker Schalk Burger defied the years with a barnstorming display of ball-carrying and tackling. Vintage stuff.
Wales boss Warren Gatland: "We don't want to make any excuses. South Africa won... and we weren't good enough to win.
"At the end of the day South Africa did what South Africa do. They got one chance and they took it and that was the result."
Read more from Gatland here.
South Africa: Le Roux; Pietersen, Kriel, De Allende, Habana; Pollard, Du Preez (capt); Mtawarira, B du Plessis, Malherbe; De Jager, Etzebeth; Burger, Louw, Vermeulen.
Replacements: Strauss for Du Plessis (55),  Nyakane for Tendai Mtawarira (56), J Du Plessis for Malherbe (60), Du Toit for Etzebeth (67), Alberts for Louw (67), Lambie for Pollard (76), Serfontein for Kriel (67).
Unused: Pienaar
Wales: Anscombe; Cuthbert, Morgan, Roberts, North; Biggar, G Davies; Jenkins, Baldwin, Lee; Charteris, AW Jones; Lydiate, Warburton (capt), Faletau.
Replacements: Owens for Baldwin (56), James for Jenkins (55), Francis for Lee (55), Davies for Charteris (63), Tipuric for Lydiate (67), L Williams for G Davies (70), Priestland for Biggar (73), Hook for Morgan (67).
Fourie du Preez's 75th-minute try ended battling Wales' World Cup dream in a colossal quarter-final against South Africa at Twickenham.