Relegated from the Bundesliga for the first time back 2017-2018, Hamburg needed to better Heidenheim's result to book a place in a play-off with Nordderby rivals Werder Bremen, who finished 16th in the top flight.
Heidenheim was beaten 3-0 by champions Arminia Bielefeld but Hamburg could not get the job done at home against mid-table opposition.
They were 2-0 down at the break through Rick van Drongelen's own goal and Kevin Behrens' 21st-minute effort.
Aaron Hunt pulled one back from the penalty spot but Behrens converted from 12 yards himself in the 84th minute to make it 3-1 before Mario Engels and Dennis Diekmeier rounded off a rout.
Heidenheim will now face Werder in a two-legged play-off. The first leg takes place on Thursday with the return leg held four days later.
Defeat for Heidenheim meant Stuttgart's 3-1 home reverse at the hands of Darmstadt was immaterial.
Mario Gomez equalised in his final Stuttgart game after Serdar Dursun had given Darmstadt the lead. Matthias Bader and Tobias Kempe sealed the points for the visitors but Stuttgart make an immediate return to the Bundesliga following relegation last season.
By contrast, Nurnberg - relegated with Stuttgart in 2018-19 - face a play-off to avoid dropping into the third tier. Their 1-1 draw with Holstein Kiel was not enough to secure safety as Karlsruher moved fourth bottom on goal difference with a 2-1 win at Greuther Furth.