Slovakian midfielder Hamsik opened the scoring on 15 minutes when he controlled an Ezequiel Lavezzi cross and fired into the corner after a defensive slip before Uruguay's Cavani struck home the perfect penalty after Lavezzi had been tripped.
Nilmar had Villarreal's best chances as the visitors pressed but the Brazilian striker left his shooting boots at home, poking wide when clear on goal just before half-time as the Spanish side suffered their second straight defeat.
Napoli, who face leaders Bayern Munich in their next fixture, go second in Group A with four points while Villarreal stay rock bottom. The Naples club drew 1-1 in their opener at Manchester City, who lost 2-0 at Bayern on Tuesday.
"We approach every game in the same way and the players are following the game plan to the letter," Napoli coach Walter Mazzarri told Italy's Sky Sport channel.
"There are very few occasions when we enter the pitch not playing as a squad. It's first for the team and then for yourself. Up to now these players have followed me and I'm proud of that."
Mazzarri admitted Napoli suffered in the dying stages.
"We were tired in the second half. [Gokhan] Inler and [Walter] Gargano have run themselves into the ground," said the Italian.
"We couldn't manage at such a high rhythm and we conceded ground. If we only played once a week or had more experienced players like [AC Milan's Alessandro] Nesta or [Clarence] Seedorf, we could do it for 90 minutes."
Villarreal, who had knocked Napoli out of the Europa League last season, began well with Cristian Zapata and Cani going close with shots within the first 10 minutes.
But fired up by an impassioned 60,000-capacity crowd in the first match in Europe's top competition at the thunderous San Paolo since the days of Diego Maradona in 1991, the home side took charge.
On 12 minutes Lavezzi almost took the roof off the stadium, following up a speedy dribble with an opportunistic left-foot strike that flashed just over.
Three minutes later though and the crowd was on its feet after Hamsik broke the offside trap to control Lavezzi's centre before calmly slotting the ball into the far corner.
Napoli doubled their lead shortly afterwards when Lavezzi was carelessly fouled by Gonzalo Rodriguez with Cavani stroking the penalty across into the corner.
Villarreal, who had lost at home to Bayern in their opening group game, had two good chances before half-time to get back into the match.
On 27 minutes, Nilmar shot weakly at Morgan De Sanctis from just inside the area before the Brazilian striker squandered a glorious opportunity five minutes later, toe-poking Cavani's wayward backpass wide of the post with the goal at his mercy.
Italian forward Giuseppe Rossi, who had passed a late fitness test, headed straight at De Sanctis from a good position on 50 minutes as Villarreal continued to pour forward in a frantic second half.
But despite winning a succession of free-kicks around the edge of the box, the Yellow Submarine was unable to find the target as the Neapolitan fans tasted triumph at the top European table for the first time in a generation.
"It was disappointing to concede two goals early on," Italian Rossi told Sky.
"We played well after that but couldn't find that crucial goal. We need to take the good things out of today's match and see if we can turn things around in the group.
Get the best features, fun and footballing frolics straight to your inbox every week.
Thank you for signing up to Four Four Two. You will receive a verification email shortly.
There was a problem. Please refresh the page and try again.