Article continues below
South Africa captain Fourie du Preez broke Welsh hearts by scoring a try five minutes from time to book the Springboks a World Cup semi-final place at Twickenham.
Wales led by a point, courtesy of scrum-half Gareth Davies' first-half touchdown, three penalties, a drop-goal and conversion from Dan Biggar, but Du Preez struck when he sprinted clear from a close-range scrum and the two-time world champions prevailed 23-19.
It was rough justice on Wales, who were World Cup semi-finalists in New Zealand four years ago, after they gave as good as they got for almost all of an unforgettable last-four clash.
After beating South Africa in Cardiff 11 months ago, Wales did not lack belief, yet the Springboks continued their impressive recovery from losing against Japan just four weeks ago by posting a fourth successive win in the tournament.
Wales dominated for long periods, and it looked as though they might stay alive despite a crippling injury list that saw them lose six of their original World Cup squad, but they were undone by Du Preez's try, plus five penalties and a drop-goal from Springboks fly-half Handre Pollard.
Several Wales players were strewn across the pitch at the final whistle, emotionally and physically spent, yet they will return home on Sunday with heads held high, knowing they could not have given any more.
Wales made a blistering start, storming into South Africa territory after impressive centre Tyler Morgan stole possession and sparked a move that ended with wing George North being held up just short of the line.
Wales, though, still possessed the creative instinct to move the ball wide, but prop Gethin Jenkins' pass flew over the head of an unmarked Morgan and a gilt-edged opportunity went astray.
An ability to cash in when Australia were temporarily reduced to 13 men in last Saturday's Pool A decider cost Wales, and they again proved wasteful after doing all the hard work that set up such promising opportunities.
Pollard kicked two penalties during a four-minute spell, and although Biggar reduced the arrears, Wales' indiscipline again surfaced, allowing Pollard to complete his penalty hat-trick.
But Wales responded magnificently through Biggar, who caught his own kick deep into South Africa heartland, then delivered a scoring pass to Davies that resulted in him scoring his fifth try of the tournament.
It was a brilliantly executed ploy by Biggar, who along with Australian Bernard Foley has been the competition's outstanding fly-half, and, although Pollard rapidly put South Africa back in front with his fourth penalty, Biggar returned for more.
The Ospreys playmaker saw a long-range penalty attempt bounce back off the post, yet with the final play of a superb opening 40 minutes, he landed a drop-goal and Wales trooped off 13-12 ahead.
Biggar's third successful penalty took Wales four points clear seven minutes into the second half, but that was the cue for a prolonged spell of Springboks pressure.
Flanker Schalk Burger was continually used as first receiver to pump holes in the Welsh defence, yet Wales stood firm, with number eight Taulupe Faletau proving a defensive colossus and skipper Sam Warburton continuing to pilfer turnover ball.
A 52nd-minute Pollard drop-goal brought South Africa back to within a point, and there were signs that the Springboks were starting to fancy it up front, but a missed Pollard penalty meant they remained a point behind on the scoreboard.
Wales continued to defend heroically, yet it came at a price when hooker Scott Baldwin was forced off after taking a blow to the head, and Ken Owens replaced him.
Head coach Warren Gatland had already sent on both substitute props - Paul James and Tomas Francis - meaning Wales had a new front row for the final quarter of a punishing encounter.
Pollard's fifth successful penalty edged South Africa ahead, but the lead lasted less than two minutes as the Springboks forwards infringed and Biggar landed an angled penalty to give Wales a 19-18 advantage with 17 minutes left.
The closing stages were inevitably fraught and riddled with nerves in both teams, yet as the clocked ticked down, so South Africa gradually increased the tempo up front, asking serious questions of Wales.
It was a case of all hands on deck for Gatland's men, but their resolve was finally broken five minutes from time.
Biggar, who looked far from happy, went off injured, and South Africa capitalised immediately, launching a blindside move from an attacking scrum and Du Preez touched down to take them into the semi-finals and send Wales out.