Inler and Hamsik fire Napoli into last 16
The Italians finished second in Group A behind Bayern Munich, with the Germans falling 2-0 at third-placed City who needed to win and hope Napoli drew or lost.
Swiss midfielder Inler struck from 25 metres out in the 65th minute to settle his nervous team-mates who had struggled to make headway against already eliminated Villarreal.
Slovakian international Hamsik bundled the ball in from close range at a corner in the 76th, leaving their Spanish hosts, semi-finalists in 2006, without a point in their six group games.
"We are a well-rounded team and we have a lot of running in us. We like to get the ball and play football and this was key to us passing to the next stage," Napoli's Argentine forward Ezequiel Lavezzi told Spanish television.
"To be in a group as difficult as this makes it particularly sweet to progress. It might be historic for us but we want to go further.
"I wasn't interested in what Manchester City were doing during the game. We were determined to concentrate on what we were doing and winning our game, and luckily we achieved that," he said as a noisy contingent of travelling Italian supporters celebrated in the background.
Villarreal striker Marco Ruben fired against the post after only five minutes at the Madrigal but there was little to separate the sides in an evenly-balanced match.
Napoli appeared to be spurred on by news Manchester City had gone 1-0 up against Bayern as halftime approached, a ripple of cheers running round the stadium, and Hamsik sent a shot fizzing past the post.
As news filtered through that the Premier League leaders had scored a second just after the re-start, Napoli coach Walter Mazzarri was sent to the stands for pushing Nilmar when the Brazilian striker stumbled into his technical area.
Inler finally struck from distance to set Napoli on their way and Hamsik reacted quickest for the second when a poorly struck corner squirmed across the face of goal and he turned the ball over the line.
"We played a good game, but perhaps they needed it more in the second half. They played well and you can see they are a very good team," Villarreal midfielder Bruno Soriano said.
"We have experience in the Champions League to have done more but it is clear it was a very difficult group. Now we have to focus on La Liga."