Mere clock ticks from elimination after 90 minutes of scoreless soccer, the all-time leading scorer in U.S. national team history buried a rebound off of a Clint Dempsey shot in stoppage time and gave the Americans a dramatic 1-0 victory over Algeria at Loftus Versfeld Stadium on Wednesday.

In an underwhelming game where they missed chance after chance, the U.S. looked headed to early elimination. But Donovan’s dramatic strike in stoppage time not only saw the U.S. advance to the knockout rounds of the 2010 World Cup, but they also finished atop Group C thanks to England’s 1-0 win over Slovenia.

Donovan raced to the corner flag and was dog-piled by his teammates as the U.S. bench erupted. The Americans finish the group stage with a 1-0-2 record, their five points enough to win Group C on the goals scored tiebreaker thanks to England’s win in Port Elizabeth.

The U.S. now will meet Ghana, the second-place finisher in Group D, on Saturday at Royal Bafokeng Stadium in Rustenburg.

Early on, however, it didn’t look good for the U.S. They got an ugly taste of déjà vu, nearly giving up another early goal and seeing another strike of their own waived off in controversial fashion.

In the sixth minute, Algeria took advantage of some confusion in the back line when Madjid Bougherra’s long ball was tapped by Karim Matmour to Rafik Djebbour. The striker trickled free of Steve Cherundolo and suddenly found himself all alone in front of Tim Howard, but his shot rattled off the crossbar.

That gave the U.S. some breathing room, and they began to make cautious, controlled build-ups with the ball. In the 21st minute, the Americans thought they had struck first. Herculez Gomez grabbed his own point-blank rebound and passed across the box to Dempsey, who tapped it in. But the goal was disallowed as Dempsey was ruled offside by a hair in a call that will be discussed heavily in the months to come.

The Americans kept trying to create without ceding too much ground but failed to finish again and again. In the 37th minute, Jozy Altidore muffed a sitter way over the crossbar after a nice ball inside the box from Donovan.

After halftime, Dempsey moved up top in place of Gomez, and he, too, missed an easy one. In the 57th, he clanged a shot off the right upright after picking up the ball just outside the box, then put his own rebound over the goal.

Second-half sub Edson Buddle got into the act, too, heading a corner just wide in the 66th minute, then putting one right at Algerian keeper Raïs M’Bolhi two minutes later. But despite having the bulk of the chances, the U.S. simply could not get the ball into the net.

But with time running out, the Americans finally found their hero. With the U.S. knowing their survival was on the line, Donovan took a throw from Tim Howard, ran up the right side, and fed Altidore. His cross was met by Dempsey, whose shot was saved by M’Bolhi. But the trailing Donovan fired in the rebound for the finish, and bedlam ensued.