Portugal knew only a win combined with a defeat for Germany or the United States in the other Group G fixture, plus a significant swing in goal difference, would take them through to the last 16.
Germany won the group by beating the US, who also qualified on goal difference over Portugal.
Although Ronaldo netted his first goal of the tournament, the winner, it was his numerous misses that cost Portugal the sizeable victory they needed to make up for the 4-0 mauling they suffered by Germany in their first game.
Portugal had taken the lead through a John Boye own goal before Asamoah Gyan equalised.
The Europeans broke the deadlock after half an hour with an own goal by the unfortunate Boye, who diverted a cross by Miguel Veloso off his knee and over despairing goalkeeper Fatawu Dauda.
The move that led to the goal started when the ball hit the referee in the middle of a promising-looking attack by Ghana, missing two of their star players after bust-ups with management.
Kevin-Prince Boateng and Sulley Muntari had been sent home after attacking and abusing, respectively, members of the Ghana management.
Ronaldo, who last month helped Real Madrid to a 10th Champions League title, had already come close to scoring on a number of occasions before the first goal.
After a scrappy opening few minutes he picked the ball up wide on the right and hit a wayward cross that sailed over Dauda only to come back off the crossbar.
He then tried his luck with a free-kick from 25 yards in a central position which Dauda beat away.
Moments later Portugal goalkeeper Beto kept a close-range shot from Gyan out with his legs as the action swung from end to end.
The Black Stars put pressure on Portugal after the break, with captain Gyan producing a shot from just outside the penalty area that skimmed narrowly wide.
Shortly before the hour mark Ghana’s persistence earned reward when Gyan headed home at the far post from a sublime cross by Kwadwo Asamoah, delivered with the outside of his left boot.
Gyan thus became the top African goalscorer at World Cups with his sixth effort in finals.
Just four minutes later the African team should have been ahead after Majeed Waris squandered a glorious headed chance from close range as the Black Stars began to get on top.
Ronaldo kicked the air in frustration as another attack foundered, with news filtering through from soggy Recife that Germany were 1-0 up against the United States, meaning both Ghana and Portugal were back in the mix.
And Portugal’s winner came in heartbreaking fashion for Ghana with 10 minutes to go after Dauda pathetically pawed at a dropping ball, giving it straight to Ronaldo to fire home low into the corner.
It was his 50th goal for Portugal and he became the first player from his country to score at three World Cups.
But he still had time to hit a tame shot from a good position straight at Dauda before incredibly knocking a cross from Nani over the bar from just four yards out.
Ronaldo had one more glorious chance when put through one-on-one with Dauda, but he shot straight at the keeper.