Hamburg were thrashed 5-1 by Sandhausen on the final day of the 2. Bundesliga as they missed out on a promotion play-off, but Stuttgart are back in the top flight despite losing to Darmstadt.
Relegated from the Bundesliga for the first time back 2017-18, 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 were beaten 3-0 by champions Arminia Bielefeld but Hamburg could not get the job done at home against mid-table opposition.
Hamburg manager Dieter Hecking made two changes from the side who lost last Sunday to Heidenheim, with the injured Timo Letschert being replaced by Ewerton in the middle of defence
Jordan Beyer also took a place on the bench, as Gideon Jung moved into the back three, leaving space for Adrian Fein to move back into one of the two defensive midfield slots.
They were 2-0 down at the break through Rick van Drongelen's own goal – deflecting a cross by Julius Biada past Julian Pollersbeck - and Kevin Behrens' 21st-minute effort after Hamburg failed to clear a simple long ball.
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.