Belgium scored in the 88th minute of a pedestrian 1-0 win over Russia leaving Algeria to light up Group H by routing South Korea 4-2.
Portugal had looked on their way out of the tournament until the fifth minute of added time in the Group G clash when Ronaldo's curling cross from the right was met with an emphatic header by substitute Silvestre Varela past goalkeeper Tim Howard.
The Americans were seconds away from a last 16 place with goals from Jermaine Jones and Clint Dempsey turning things around after Nani's early strike for the Portuguese in steamy Manaus.
"When you concede in the last second it is unfortunate, but it was an amazing game from us, I can't ask for any more. Now we have to get a result against Germany and that is what we are going to do," said U.S. coach Juergen Klinsmann.
Germany and the U.S. have four points with Ghana and Portugal on one apiece going into the final games on Thursday. Germany take on the Americans in Recife with Ghana and Portugal meeting in Brasilia.
Belgium teenager Divock Origi became the youngest scorer so far at this tournament when he secured victory with a sharp shot into the roof of the Russia net from Eden Hazard's devastating run down the left and pinpoint cutback.
Algeria, who lost 2-1 to Belgium in their first match, tore the ramshackle Koreans apart, scoring the highest number of goals by an African team in a World Cup match.
It was the joint second-highest scoring match of the Brazil finals so far after France's 5-2 victory over Switzerland and equalled the Netherlands' 5-1 thumping of title holders Spain.
Islam Slimani and Rafik Halliche stunned the Koreans with two goals in two minutes and Abdelmoumene Djabou added a third to give Algeria a 3-0 lead at the interval.
They became the third team to score three first-half goals at this tournament after Germany in their 4-0 win over Portugal and France in their rout of the Swiss.
South Korea pulled one back through Son Heung-min, who turned to shoot between goalkeeper Rais Mbolhi's legs in the 50th minute.
Yacine Brahimi scored the goal that set the African record just after the hour and took the steam out of the Korea rally before Koo Ja-cheol pulled another goal back.
Algeria go into their last group match against Russia on Thursday in second place with three points, two more than both Fabio Capello's side and the Koreans, looking to reach the last 16 for the first time.
"We did analyse the strategies but the result was such that I could say it was not well done," South Korea coach Hong Myung-bo told reporters.
"The result speaks for itself... Since we conceded a lot of goals, there was a strategic mistake on our part."
Once Belgium's celebrations are over, coach Marc Wilmots will need to reflect on their scoring difficulties if they are to confirm their status as dark horses in the knockout phase.
The match at the Maracana, which rivalled Nigeria's 0-0 draw with Iran as one of the worst in a tournament which has been mostly thrilling, was brightened up by Origi's late strike.
Capello was pleased with the performance of Russia, who host the next finals in 2018, saying they did not deserve to lose.
Wilmots was not too bothered with the manner of the victory and, along with Hazard, said fitness was the key.
"To advance in a tournament, you don't always need to be pretty. I believe that you need to be efficient," said Wilmots, who played for Belgium at three World Cup tournaments.
"There's something clear in football. Levels of physical fitness count in a World Cup." he added.
"I know how to make the difference. When I make the difference, I often do it at the end of the match and that shows that I am fresh," said Hazard.