Captain Greig Laidlaw scored the decisive try in the final seven minutes, though the Scots had to cling on following Motu Matu'u's late score.
Samoa scored through Tusi Pisi, Ma'atulimanu Leiataua and Rey Lee-Lo in a thrilling first half.
Tommy Seymour and John Hardie crossed for the Scots before the break.
Scotland also had Ryan Wilson sin-binned in the first half for stamping.
The Wallabies beat the Welsh 15-6 at Twickenham and will now face Vern Cotter's Pool B runners-up, with South Africa having already won the section.
Same old Scotland
All week Scotland had spoken of their need to get out of the blocks a whole lot quicker than they have done so far in the tournament. They didn't. They were an unmitigated shambles. Incredibly slow at re-starts, passive in the tackle, hugely vulnerable out wide and beaten in all parts of the pitch.
Samoa were thunderous and clever and accurate and Pisi, Leiataua and Lee-Lo crossed with aplomb. Scotland were bullied. At the break they were in trouble. Big trouble.
Seymour intercepted almost on the try line to touch down after Pisi's opener and Hardie forced his way over after the Samoans' third score, with Laidlaw's kicking keeping the Scots in touch.
Same old Samoa
Scotland brought a better physicality in the second half. Better scrum, better attitude. Just. Under pressure, Samoa reverted to type and conceded penalty after penalty.
Their indiscipline in the heart of the battle was horrific. Quite how they avoided a yellow card is a mystery.
That, combined with a solid scrum from Scotland, created the platform for Laidlaw's pivotal score. Even then, there were monstrously fretful moments, with Matu'u bustling over soon after.
But Scotland survived and move on to the last eight for the first time since 2007.
Scotland head coach Vern Cotter said the match had been about "winning and fighting for a win".
"If anyone thought it was going to be an easy game, after the first five minutes, we certainly got a taste of what was coming at us," he said.
"We found ways into the game and I thought the forwards stepped up and played well. I'm really, really happy for them."