Wales will face South Africa in the World Cup quarter-finals next weekend after suffering an 11th successive defeat against Australia.
The Wallabies, meanwhile, face a last-eight appointment with Scotland after five Bernard Foley penalties helped them overcome what appeared to be insurmountable odds at Twickenham.
Australia spent seven minutes of the second period down to 13 men after scrum-half Will Genia and lock Dean Mumm were both yellow-carded.
But the Wallabies then produced arguably the most remarkable defensive rearguard in World Cup history, defying their two-man disadvantage to repel wave after wave of Welsh attacks.
Wales, for whom fly-half Dan Biggar booted two penalties, were partly their own worst enemy, as they lacked the tactical awareness and skill to unlock a team so far on the back foot they were almost down the road in Richmond.
But, in a totally different display from last week's 33-13 demolition of England, Australia showed stunning defensive resilience, and they must go into World Cup knockout rugby next week as clear favourites to secure a third world title.
Wales, who last beat Australia seven years ago, were once again left to fret about what might have been, although they will take heart from the last time they faced South Africa 11 months ago, skipper Sam Warburton and his men claimed victory.
They will also need to pick themselves up psychologically, though, considering their attacking deficiencies and a lack of tactical appreciation to break down outnumbered opponents.
Wales were quickly into their stride, delivering a couple of early strong scrums, while they also went close to a try before Biggar opened their account with a fifth-minute penalty.
It was all Wales in the opening exchanges, but they were unable to make their territorial advantage count as Australia grew into the contest and began to gain scrummaging confidence following a stodgy start.
With referee Craig Joubert eager to readily punish both sides, allowing them little margin for error, Foley pulled Australia level through a 24th-minute penalty before edging them in front six minutes later.
There were increasing problems for Wales, who had got on Joubert's wrong side, and after Biggar's second successful penalty made it 6-6, the South African official handed Warburton's men a final warning.
Number eight Taulupe Faletau was the guilty party, allowing Foley a penalty hat-trick chance that he duly accepted, but Biggar stepped up during the final minute of a frantic opening period and missed his first kick of the tournament at the 16th attempt.
Wallabies centre Matt Giteau was then well short with a speculative long-range penalty during the first-half's dying seconds, and Australia trooped off 9-6 ahead.
Foley increased Australia's advantage to six points with a 50th-minute penalty after Faletau was penalised for a ruck offence, and the Wallabies had undoubtedly established a small degree of control.
Many of the 50-50 decisions continued to go against Wales, yet they knew the game was far from over, being less than a converted try behind.
Gatland made his first substitution 14 minutes into the second period, replacing prop Samson Lee with Exeter tighthead Tomas Francis, as his team looked to assert some authority.
And they were helped hugely when Australia lost Genia and Mumm for technical offences during the space of three madcap minutes, which piled the pressure on Wales to make a two-man advantage count.
But Australia were magnificent under almost unbearable pressure, and it was Wales who ended the game a man short when wing Alex Cuthbert saw yellow for a deliberate knock-on.
Foley could not punish him by landing his sixth successful penalty from six attempts, yet Australia had done enough, giving them a seemingly easier semi-final route and leaving Wales to encounter South Africa's relentless physical ferocity in just seven days' time.