Paulo Bento’s side had to win by a big margin against their African opponents and needed the USA to lose to Germany in the other Group G match.
Although the Americans suffered a 1-0 defeat in Recife, it was not enough for Portugal to finish second and progress to the last 16.
The Seleccao came third in the group level on four points with the USA, but with an inferior goal difference.
Portugal looked more creative from the start, with Cristiano Ronaldo hitting the crossbar with a brilliant shot from a narrow angle on the right.
Ronaldo had a header saved by Ghana goalkeeper Fatawu Dauda as the first half progressed, before the European side surged ahead under fortuitous circumstances.
A good attacking move from Portugal saw Miguel Veloso receive the ball on the left flank, and his cross was inadvertently put into his own net by John Boye while trying to clear the danger.
Despite Portugal’s early pressure in the second half, Ghana equalised on 57 minutes when Asamoah Gyan headed home Kwadwo Asamoah’s cross from close range.
However, the Euro 2004 finalists had the last laugh in the contest as Ronaldo scored his side’s second and winning goal with 10 minutes of normal time left to play, smashing the ball in from close range.
Despite their World Cup fate being largely out of their hands, Portugal were determined to get the win they needed to keep their slim hopes alive and were unlucky not to be in front after five minutes when Ronaldo pumped in a cross-shot from the right wing which rattled the crossbar.
The Real Madrid forward threatened again seven minutes later from a free kick, which was lifted over the wall but punched away by Ghana goalkeeper Fatau Dauda.
Ghana responded by launching a couple of attacks of their own, but attempts to get the ball to dangerman Gyan were dealt with by the Portuguese defence.
But it was Bento's men who seemed the more likely to score and another chance went begging for Ronaldo after 19 minutes, when his point-blank header from Joao Pereira's cross was bettered by a class save on the line from Dauda.
Ghana immediately worked the ball down the other end, where Gyan's shot was kept out by Beto's feet. And the Portugal goalkeeper made another contribution minutes later, getting his hands to a header from Gyan after the former Sunderland striker met a free kick from Asamoah.
However, Portugal eventually found the goal they wanted after 31 minutes, although it was a fortunate opener.
Miguel Veloso was the creator, whipping in a cross from the left which bounced horribly for Boye and resulted in the defender slicing into his own net.
Ghana responded to the challenge, with Christian Atsu's assist headed past the right post by Gyan, before Atsu himself tried a curling effort from the left side of the area which fell just wide of the far post as the Africans sought an equaliser.
Yet it was Boye who almost added to his tally on the stroke of half time, a goal which would have extended Portugal's lead as the Rennes defender sliced a ball into the area from Nani narrowly over the bar.
The Africans came out a stronger force after the interval and were finally level after 57 minutes thanks to a fantastic ball in from Asamoah, with the Juventus midfielder using the outside of his left boot to thump in a pass which Gyan finished with a neat header.
The goal was Gyan's sixth in World Cup finals and saw him become the all-time leading African scorer in the tournament, passing the previous mark set by Cameroon's Roger Milla.
Portugal were beginning to look vulnerable and better finishing from an unmarked Abdul Waris would have put Ghana in front shortly after the hour, but the forward failed to make the most of a floated cross in from Gyan and directed his header past the right post.
Instead it was Portugal who finally broke the deadlock 10 minutes from time. Again luck was on their side as Nani's cross was headed high into the air by Jonathan Mensah, with Dauda flapping at the ball and pushing it straight into the path of Ronaldo, who slammed home for his first goal in Brazil.
Portugal and Ronaldo should have added to their total, but the FIFA Ballon d’Or winner was denied twice by Dauda and also turned Nani's assist over the top of the net as the opening 4-0 rout to Germany proved their undoing.