UEFA Europa League: Napoli 2 Porto 2 (2-3 agg)
Leading 1-0 from the first leg thanks to Jackson Martinez's goal, Napoli levelled Thursday's tie through Goran Pandev's first-half strike at Stadio San Paolo.
The hosts piled on the pressure and spurned several chances in search of a crucial second goal.
And Napoli were harshly punished by substitute Ghilas on the break in the 69th minute, with the Algeria striker having also scored the equaliser in a 3-3 draw against Eintracht Frankfurt that sent Porto into the last 16 on away goals.
Ricardo Quaresma then put the tie seemingly beyond doubt with a superb strike 14 minutes from time before Duvan Zapata netted a late equaliser to level on the night, but Porto progressed with a 3-2 aggregate victory that boosts interim coach Luis Castro's hopes of getting the role on a permanent basis.
Napoli head coach Rafael Benitez had described the tie as "like a final" in the build-up and his side rose to the occasion in the first half to lead thanks to Pandev's calm finish.
But Porto's quick turnaround in the second half ended the Spaniard's hopes of winning the Europa League for the second straight year, following his triumph with Chelsea in the 2013 final.
Napoli began the match at a quick tempo and created two chances in the opening 10 minutes. Dries Mertens was first denied by Fernando's fine last-ditch tackle, before Lorenzo Insigne stung the palms of Porto goalkeeper Fabiano.
Benitez's men continued to control play and full-back Henrique flashed a half-volley across the face of goal after quarter of an hour.
And Napoli's early pressure paid dividends in the 21st minute, as Benitez's men deservedly levelled the tie on aggregate.
Gonzalo Higuain held the ball up on the edge of the area before his neat throughball found Pandev's clever run into the box and the forward provided a delicate finish past Fabiano.
Porto sought an equaliser, but it took 33 minutes for the visitors to threaten Pepe Reina's goal, when Quaresma's corner was headed wide at the back post by Martinez.
The lively Insigne then threatened to put Napoli ahead as he cut inside Danilo from the left and flashed a shot wide of the near post.
The diminutive forward went close again early in the second half, but his downward header at the back post was expertly saved by Fabiano at point-blank range.
Porto had offered little going forward, yet nearly snatched a goal in the 57th minute when Quaresma's free-kick was glanced narrowly wide by Carlos Eduardo.
Napoli then had two good chances to take the lead in the tie through Higuain and Mertens before their wastefulness was duly punished.
First, Ghilas – who had replaced Silvestre Varela - sprung the offside trap to receive Fernando's superb throughball and keep his composure to slot firmly past Reina.
And, with just under 15 minutes remaining, Quaresma weaved his way through the defence before unleashing into the roof of Reina's goal.
Napoli clinched a draw on the night in injury time when Jose Callejon's ball across the six-yard box was tapped home by Zapata, but it was too little, too late for the hosts.