Until then Sweden had seemed set to earn a point from a dogged display after Zlatan Ibrahimovic had cancelled out Fernando Torres’s opener.
“We dominated the opening 20 minutes and deserved to go in front but then we lost our way a bit,” said Spain coach Luis Aragones.
“However, with six points we would normally expect to be through,” he added.
“In the second half we were the better side, we had more of the ball, we didn’t create many chances because they defended well but we had some good chances. I think we were the better team.”
Villa added to his burgeoning reputation with his fourth goal of the tournament following his treble against Russia and said his latest effort was the pick of the bunch.
“This goal gives me more pleasure than the three in the previous match. But my goals are not the most important thing – the most important thing is the team has six points,” he said.
Sweden coach Lars Lagerbäck said the defeat was tough to stomach.
“Obviously this is a bitter way to lose a match in the very final seconds,” he said.
“We had an extremely good first half and had more chances than Spain even if they had a lot of the ball and created some very good chances.
“They’re the best passing team in world, they’re an extremely good team and I have to congratulate my players even though this is a bitter defeat.”
Spain enjoyed some success in getting behind the Sweden defence in the early stages but they could only find yellow shirts with their crosses as the Scandinavians remained stout at the back.
However, the Iberians took the lead with a corner routine of sublime intricacy on 15 minutes.
Villa darted out of the six-yard box to receive a short corner from Xavi and transfered the ball to David Silva, whose marker had been drawn to the ball, on the edge of the area.
Silva crossed and Torres stretched out a leg in front of his marker Petter Hansson to poke the ball home at the near post.
That drew an instant response from Sweden as Johan Elmander took a return pass from Henrik Larsson and burst into the area only to hit the side netting from a tight angle.
Having been on the back foot for most of the opening stages, Sweden now came more into the match and a brilliant through ball from Ibrahimovic over the defence found Larsson’s angled run but, off balance, he lofted his shot over the bar.
Sweden were in the ascendancy and deservedly equalized on 34 minutes through Ibrahimovic, who took down a deep cross from full-back Fredrik Stoor, held off the back-tracking Sergio Ramos and curled home a low shot despite Iker Casillas getting a hand to it.
Sweden survived a penalty shout in the last minute of the half as Elamnder barged into the back of Silva but Dutch referee Pieter Vink waved away Spain’s protests.
The Vikings suffered a blow as Ibrahimovic, who had been struggling with a knee problem in the build up, failed to come out after the break, replaced by Markus Rosenberg.
Sweden set out their stall to defend and counter while Spain were guilty of overplaying the ball around the area, particularly between Silva and Villa.
Aragones replaced Xavi and Andres Iniesta with Cesc Fabregas and Santi Cazorla but the move back-fired as Spain lost some of their fluency.
Spain had a treble chance on 63 minutes after neat interplay between Villa and Silva opened up the Swedish defence, but Silva dallied in bringing the ball onto his favoured left foot and his weak shot was parried by Andreas Isaksson who then got up to block Villa’s follow up before Torres had a shot cleared by Daniel Andersson.
Marcos Senna then forced a fine diving save from Isaksson after beating three men in midfield.
With 11 minutes remaining Larsson came within inches of turning home a ball from Hansson across the face of goal but he arrived just too late.
Right at the death, though, Villa latched onto a stunning long ball from Joan Capdevila to slide the ball past Isaksson and send Spain’s fans into jubilation.