But Sweden responded well and levelled when Zlatan Ibrahimovic met Fredrick Stoor's cross on 34 minutes.
Spain, who could have had a penalty when Johan Elmander barged Silva, were frustrated by Sweden keeper Andreas Isaksson before Villa's neat finish.
Spain were far less stylish than they had been in despatching Russia in their opening match.
But Villa's goal, his fourth of the tournament, was still enough to put his side into the quarter-finals.
Spain will also top Group D and avoid the in-form Netherlands in the last eight.
Spain coach Luis Aragones said: "We're happy. We had many chances in the second half.
"In the second half, we went out looking for the goal and things worked out. We were the boss of the ball in the second half."
Spanish centre-back Carles Puyol was forced off after 25 minutes and Aragones will have to wait to discover the extent of his foot injury.
"I hope he won't be out for too long," said the Spain coach. "We just hope it's not very serious.
"I haven't been able to talk to the medical doctor yet."
Left-sided midfielder David Silva has a similar injury but Aragones added: "He has a slight problem but I hope I won't have to replace him."