La Roja will join Croatia, Italy and the Netherlands in the Nations League finals - though for much of Tuesday's game it didn't look like they'd get the job done.
Spain had barely created anything of note throughout, but in the 88th minute, Alvaro Morata popped up to crash home Nico Williams' clever knockdown and send his country top of League A2.
Out-of-form Ronaldo had his side's two best chances in front of goal but he spurned them both, firing straight at the goalkeeper on each occasion as his alarming struggle for form continues.
This was hardly a ringing endorsement for either side's World Cup chances either, with Spain often drearily stroking the ball around without any penetration while their opponents were worryingly toothless in front of goal.
The visitors monopolised possession in the opening stages without really imposing themselves, but Portugal had the better chances, with Ruben Neves, Diogo Jota and Bruno Fernandes all having opportunities the first half.
The second period started at some pace but would ultimately follow a similar pattern. Ronaldo spurned a glorious chance just two minutes in, firing straight at the goalkeeper from close range after being slipped in by Jota.
Eventually, Portugal's wastefulness was punished with two minutes to go, as a diagonal pass was headed back across goal by Williams and Morata slammed the ball into the roof of the net from mere yards out.
Ronaldo had one more chance to level, but his fierce strike from a tough angle was straight at Simon at his near post.