(CNN) -- The United States and England are through to the last 16 of the World Cup after a dramatic finale to Group C on Wednesday.
England saw off Slovenia 1-0 while the Americans, who looked to be going out of the tournament, scored an injury-time winner through Landon Donovan to beat Algeria 1-0 in Pretoria.
A draw would have let Slovenia qualify in second place behind England, but the win put the U.S. top of the group standings, having scored more goals.
All four teams had a chance of qualifying going into the final round of matches, with Slovenia -- the country with the smallest population at the World Cup -- having been in pole position.
United States 1-0 Algeria
L.A. Galaxy star Donovan started the move for the winning goal with a long pass to Jozy Altidore, whose shot was blocked by Algerian goalkeeper Rasis Bolhi.
But Donovan was there on the follow-up to sidefoot home the vital goal.
There was still time for Algerian captain Anther Yahia to be sent off for a second yellow card, and at the final whistle it was the U.S. celebrating.
The U.S. had looked set to rue another refereeing decision that went against them as Fulham's Clint Dempsey had the ball in the net in the first half, but it was ruled out for a debatable offside.
Algeria hit the woodwork before Dempsey's effort was ruled out, with Rafik Djebbour crashing a shot against the crossbar in the fifth minute.
Dempsey also hit the post for Bob Bradley's men in the second half, and time looked to be running out until the late drama.
"I've been on a long journey these last four years and today I am so stoked and proud," Donovan told AFP.
"Algeria were pushing and we had to take the fight to them. Time felt like it stopped when I scored but I just couldn't miss it from there.
"We aren't done yet. We had a bad decision against Slovenia [when a late goal was ruled out], but we are Americans. We get on with it, we don't complain."
England 1-0 Slovenia
After two draws against the U.S. and Algeria, the pressure on Fabio Capello's England in Port Elizabeth was enormous, but after an uncertain start a 22nd-minute goal from Jermain Defoe eased their nerves and ultimately settled the match.
James Milner, who with the Tottenham striker was one of three changes in the England team from the goalless bore against Algeria, supplied the perfect cross from the right for Defoe to volley home.
England might have added a second as another Milner cross caused chaos in the Slovenian defense and Frank Lampard blazed over a left-foot shot with the goal gaping.
Defoe was then sent through and his snapshot was parried by Samir Handanovic out to Wayne Rooney.
The Manchester United star found England captain Steven Gerrard, and his shot nearly squeezed its way past Handanovic as he grasped the ball at the second attempt.
England started the second half brightly and Rooney came agonizingly close to his first goal of the finals as he picked up a pass from Frank Lampard only to see his shot hit the post with Handanovic getting a vital touch.
Rooney was later replaced to give Chelsea's Joe Cole his first taste of the finals, but he will get the chance to show his true quality in the knockout stages.
For Capello, the result was a vindication after being at a loss to explain England's poor displays in the two earlier matches.
"This was the team that I know," he told BBC Sport
"This team can go forward. All the team the coaches, their mind is free. We can go forward without fear, without everything."