After coming within minutes of elimination, the United States topped its group at the World Cup for the first time since 1930, leading Group C with five points. England also progressed with five points, but with an inferior goal difference, after its 1-0 win over Slovenia at Port Elizabeth.
Donovan slotted in the rebound after Algeria goalkeeper Rais Bolhi had stopped Clint Dempsey's shot from close range in the first minute of injury time. It was his fourth World Cup goal, equaling Bert Patenaude's U.S. record.
"I'm shocked. I'm so proud of our guys. Unbelievable," Donovan said. "Clint made a good run in the middle, the ball fell to me ... time kind of stopped. You can't miss from there."
Both teams had chances but failed to score during regulation time in the free-flowing match at Loftus Versfeld Stadium in Pretoria. A 0-0 result would have eliminated both teams, as England had just completed its win over Slovenia, which had four points going into the last match.
So it set up a tense finish for the Americans, who held England to a 1-1 draw and staged a dramatic comeback from two goals down to level at 2-2 with Slovenia in its previous matches.
As the final whistle sounded against Algeria, the U.S. substitutes and staff raced onto the field and piled on top of the players on the far sideline.
Donovan said he'd never despaired, even when the Americans hadn't been able to convert chances in regulation time.
"We believe man. We're alive baby!" said Donovan, in tears at times after the match.
Coach Bob Bradley said his U.S. team had to take risks, pushing hard for a goal to secure the win and making for a wide open game.
"We're proud," he said. "We finished first in our group. Five points. Didn't lose a match. So we're ready.
Dempsey had a goal disallowed for offside in the first half and hit the post in the second.
TV replays appeared to show Dempsey to be onside when he tapped into the net from close range after being fed by Herculez Gomez in the 21st minute. He hit the post in the 57th and missed the rebound.
Rafik Djebbour went close to scoring Algeria's first tournament goal in the 6th, with the striker's shot hitting the crossbar after U.S. defender Jay DeMerit failed to clear a long ball.
The action switched quickly from one end to another as Algeria tested U.S. goalkeeper Tim Howard with dangerous long-range strikes from Karim Ziani and Karim Matmour and the United States sought to break into the area with quick passing.
The break finally came for the U.S. when Donovan sent a pass to Jozy Altidore on the right flank. Altidore advanced and sent a low cross toward Dempsey, who was blocked by Bolhi. Donovan picked up the rebound and scored from 8 meters (yards).
Algeria, which has never advanced beyond the group phase in a World Cup, finished with one point from its dour draw with England and two losses.
After scoring two goals in the 2002 World Cup as a 20-year-old, Donovan virtually disappeared in the 2006 tournament, when the United States was eliminated in the first round. Then his marriage with actress Bianca Kajlich collapsed. But his career took an upswing this year during a three-month loan from the Los Angeles Galaxy to Everton.
"I've gone through a lot in the last four years," Donovan said, choking on tears, in the post-match news conference. "I'm so glad it culminated in this way. And it makes me believe in good in the world.
"When you try to do things the right way it's good to see them get rewarded."