Luis Enrique's side knew that only a victory would be enough to move ahead of Portugal at the top of League A Group 2 after suffering a 2-1 home defeat to Switzerland last time out.
Spain dominated proceedings with almost 70% possession throughout the 90 minutes, but they struggled to breach Portugal's backline until Morata made the breakthrough just two minutes before stoppage time.
La Roja, who were beaten in the 2020-21 Nations League final to holders France, join Croatia, the Netherlands and Italy in the semi-finals, which will take place next June.
Spain had scored in each of their previous 19 international games heading into tonight's fixture, but they struggled to create any clear-cut chances in the opening exchanges and failed to register a single shot on target in the first half.
Portugal, meanwhile, possessed more of an attacking threat, with Spanish stopper Unai Simon forced to keep out strikes from Ruben Neves and Diogo Jota.
Enrique knew that his side had to take more risks if they were to come out on top against a robust Portuguese outfit, and he decided to bring on three teenagers on the hour mark in the form of Pedri, Gavi and Yeremi Pino to inject some much-needed energy into his Spain side.
All three players, along with fellow substitute Nico Williams, made an impact off the bench and as Spain pushed hard for a winner in the closing stages, their pressure eventually paid off in the 88th minute when Williams nodded the ball towards Morata, who rifled a strike beyond Diego Costa from close range to send the Spanish bench into pandemonium.
The other game in League A Group 2 this evening saw Switzerland beat the Czech Republic 2-1 on home soil to secure third place and avoid relegation at the visitors' expense.
Remo Freuler and Breel Embolo scored within a minute of each other midway through the first half before the Czechs pulled one back through Patrik Schick on the stroke of half time.