Jacques Zoua put the visitors in front, but as they were enjoying their best spell of the match, they conceded an equaliser through Filip Daems.
Two quick-fire goals in the second half from Raffael and Alvaro Dominguez ended Hamburg's hopes of getting something out of the game and took the Foals to within three points of fourth-placed Bayer Leverkusen.
By losing by two goals, Hamburg slip below Stuttgart into an automatic relegation berth.
The objectives of the two sides may have been contrasting, but the stakes were equally high with Gladbach bidding to remain in the hunt for a place in the Champions League next season while Hamburg are fighting to remain in the Bundesliga.
With so much to be won or lost, the game started off as a rather cagey affair with neither side willing to make the first move.
The hosts showed a little more attacking intent while trying to draw Hamburg out to create space.
Mirko Slomka's men did not allow themselves to be lured into that trap, although when they did get forward, they were rewarded with the opening goal.
Hakan Calhanoglu underlined his free-kick prowess with a delivery which Zoua only needed to get a touch on to flick past Marc-Andre ter Stegen in the 28th minute.
The goal gave Hamburg belief and Calhanoglu sent a free-kick into the wall before Rafael van der Vaart headed just wide of the post.
The home fans were starting to get unsettled when the referee came to their rescue, awarding a penalty after Michael Mancienne handled the ball inside the penalty area.
Daems stepped up to the spot only to be denied by Rene Adler, but the Belgian followed through to make sure at the second attempt.
Not wanting to be outdone in the set-piece stakes, Juan Arango saw his first free-kick attempt hit the wall before almost setting up Christoph Kramer with his second attempt.
The Foals started to get on top of the game, and two goals in the space of three minutes saw them snatch the points.
The first, in the 75th minute, came when Max Kruse picked out Raffael with a delightful ball, and the Brazilian took the ball round Adler before scoring into the unguarded goal.
Roel Brouwers then got his head to a corner, flicking the ball on to the unmarked Dominguez at the far post to score his first goal of the season from close range, sealing victory in the process.