- Álvaro Morata (88')
Portugal had dominated the match but Spain stayed in the contest and in the 88th minute a long ball into the box from Dani Carvajal was headed by Nico Williams across to Morata, who lashed it into the net to silence most of the sold-out stadium.
- O'Hanlon: The flaws of every World Cup favorite
Spain finished top of League A-Group 2 on 11 points from six games, one point above second-placed Portugal who only needed a draw to secure a place in the semi-finals.
"It was a difficult game but I stay with the attitude of the team. We have made a great effort," Morata told reporters. "If we were knocked out it had to be by leaving everything on the field and that's how it was one more time for us.
"Every time that Spain have to show up in big games we do it. That's how we do it and tonight we did it again."
It was a bitter blow for Cristiano Ronaldo's Portugal who created several chances, were in total control during most the game and looked so close to securing a place in the finals.
The hosts almost opened the scoring in the 32nd minute through Diogo Jota, who received a long pass from Bruno Fernandes and lashed a shot towards the top right corner that Unai Simon kept out with a brilliant one-handed save.
Fernandes almost scored himself five minutes later when he rifled a cross shot into the side netting with an effort that was so close the home fans thought it was a goal.
Spain looked lost and confused in what was looking to be another lacklustre performance after they went down 2-1 at home to Switzerland on Saturday.
It was not until he made four substitutions in the second-half that Spain woke up and got their first shot on target of the game after 70 minutes through Morata.
"This is a marvellous sport. A win is the best antidote," Luis Enrique said after the match. "Portugal have top, top individual quality. In the first half we had a lot of the ball, I think 400 plus passes, but we'd prefer to have more in the opposition half.
"We corrected that in the second half. Everyone will think the second half was incredible, but the first half was necessary for that. In the second half I thought the goal would come. It's a huge joy, we're in the final four again, let's enjoy it."
Ronaldo tried hard to score but his play was often erratic and he was unable to successfully link up with his team mates, who kept missing chances and left Spain in the game, allowing Morata to be the hero again with his late winner.
Asked about who he'd take to the World Cup in November, Luis Enrique said: "It's not about 26 players, there are 40 who could go to the World Cup. We've had Borja [Iglesias] and Nico [Williams] now. It's a pleasure to have so many quality Spanish players. The shame is that some will be left out. You see that so many players deserve it and it's a shame."
Portugal manager Fernando Santos said the loss did not change his team's confidence and preparations for the World Cup, where he believes Portugal "are still one of the favorites." They face Ghana, Uruguay and South Korea in Group H.
"We were really well organised in the first half. They had possession but didn't create anything with that. However, we went down and lost the intensity after the 60th minute," Santos told a news conference.
"They grew mainly because we lost the ability to have the ball. Players could not turn on their game.
"Spain began to push and came from really having no chances to end up scoring a goal. In the final minutes they changed the script, that's evident."
- Daniele Orsato