After the break Wallabies somehow survived quickfire sin bins to Will Genia and Dean Mumm to repel Wales, who spent that entire period camped in their opposite 22.
Either side of that madcap spell Foley slotted two more penalties and Australia saw out the last ten minutes to line up a clash with Scotland in next week's quarter-finals.
In contrast Warren Gatland's men must tackle two-time World Cup winners South Africa - who have put their embarrassing opening defeat to Japan firmly behind them.
Wales traditionally have trouble toppling the elite southern hemisphere sides - indeed they had not beaten Australia in ten Tests - but none of that baggage seemed to matter early on.
Gareth Davies sniped down the blindside of a scrum and when the ball was worked right to left, last-ditch tackling prevented British & Irish Lion George North from dotting the ball down.
A five-metre scrum after an Australian knock-on didn't bring a try but Biggar, who slayed England at Twickenham two weeks ago with his boot, eventually put the first three points on the board.
Wales' early momentum showed no signs of abating and only a crunching Sean McMahon hit stopped Biggar from prising open a gap in the Australian line.
With the first quarter of an hour in the books errors began to creep into Wales' game, not least miscued kicks from Biggar and full-back Gareth Anscombe - on his first international start - that handed back the initiative.
In the 20th minute Australia finally breached Wales' 22 and Foley got Australia on the scoreboard at long last after Samson Lee was pinged at scrum-down.
The much-improved Australian set-piece continued to cause Wales problems and Foley's second penalty, when Liam Williams went off his feet, was a long time in coming.
But Wales earned a reprieve when Matt Giteau's long-range penalty died before it reached the uprights after Sam Warburton strayed offside.
Australia roared out of the blocks in the second half but again inaccuracies put paid to good play, Giteau in particular aiming a pass that only picked out the touchline.
Still, with No.8 David Pocock prowling around the breakdown Wales were finding it tough. In one such ruck Taulupe Faletau, part of an all-star Lions back-row with Warburton and Justin Tipuric, neck-rolled Scott Fardy and Foley chipped over to extend his side's lead to six.
But the momentum was flipped on its head when Davies charged down a clearance kick before running the ball back.
Genia then cynically impeded Davies who was looking for a quick tap and go, which earned the Australian scrum-half ten minutes in the bin.
Wales inched forward and Faletau let the ball slip just as he was bringing it to ground, though Wales retained the ball due to an earlier Wallabies infringement.
Australia were reduced to 13 after former Exeter Chief Mumm knocked Alun Wyn Jones off his stride at the line-out yet somehow they managed to hold firm.
The Welsh mounted a further assault and Foley somehow stopped a full-pelt North from going over in just one example of Australia's herculean defence.
All that pressure was relieved when Wales were pinged for not releasing and in the 72nd minute - with the Wallabies still a man light - Foley slotted through his fifth penalty after a lightning counter.
To end a thoroughly frustrating day for Wales, Alex Cuthbert saw yellow for a deliberate knock-on - though Foley's penalty went wide.