Bundesliga Wrap: Win lifts Stuttgart off foot
Christian Gentner struck first for the hosts but Bremen were on terms early in the second period thanks to Davie Selke.
Daniel Ginczek restored Stuttgart's lead but their afternoon threatened to unravel after Martin Harnik was sent off and Jannik Vestergaard equalised for a second time in the 86th minute.
But Huub Stevens' team claimed a third home win of the campaign when Ginczek struck in stoppage-time to dump Hamburg down to the bottom of the standings.
Stuttgart are now three points from safety, while Cologne eased clear of the drop zone by beating European hopefuls Hoffenheim 3-2.
Cologne captain Matthias Lehmann opened the scoring with a penalty midway through the first half and Anthony Ujah rifled home a second in the 54th minute.
What looked like a routine win became more complicated for the home team with 20 minutes to play as right-back Pawel Olkowski was sent off for a professional foul on Anthony Modeste in the penalty area.
Eugen Polanski netted the resulting spot kick, but Hector Jonas restored Cologne's advantage with a brilliant solo effort before Modeste got on the scoresheet himself to set up a grandstand finale.