Porto, having thrown away a two-goal lead to draw the first leg 2-2, looked on their way out after falling 2-0 down in Germany to goals from Stefan Aigner and Alexander Meier.
The visitors fought back and managed to get back on terms thanks to two Eliaquim Mangala headers, levelling the tie up at 4-4, but their good work looked to be for nothing as they conceded another goal to Meier with 14 minutes to go.
However, with time running out the Portuguese giants managed to fashion a late chance and substitute Ghilas was able to turn it home to seal a showdown with Napoli.
That equaliser also kept alive Porto's hopes of winning a second Europa League title four years - something that appeared in tatters after 52 minutes of Thursday evening's game as they found themselves trailing 2-0.
Porto started promisingly as they looked to put last week's first-leg disappointment and Sunday's shock home league defeat to Estoril behind them, but after Jackson Martinez, Mangala and Hector Herrera all spurned decent openings, Frankfurt took the lead.
Pirmin Schwegler's deep cross was headed down by Meier and Aigner was on hand to poke the ball past Helton.
That was in the 37th minute and seven minutes after the restart Frankfurt doubled their lead when Meier slotted in after three home players had broken the Porto offside trap.
Porto needed a quick response, and they got it six minutes later when Mangala dived to head home Ricardo Quaresma's inswinging cross at the near post.
That set the scene for a frenetic half-hour in which both sides created chance after chance.
Frankfurt failed to make the most of a three-on-one break, with Porto's Silvestre Varela just managing to avoid putting the ball in his own net, and then Maicon saw his well-struck shot superbly saved one-handed by home keeper Kevin Trapp
Trapp was powerless in the 71st minute though as Porto found the equaliser, with centre-back Mangala again the unexpected hero as he rose unmarked to power home a header from five yards.
With the momentum behind them, Porto looked favourites at that point, but they found themselves trailing again five minutes later when Meier volleyed home at the far post after Bastian Oczipka's cross had been flicked on by Joselu.
Once again Frankfurt had the tie within their grip, but once again Porto rallied - and this time it was decisive.
A long ball to the edge of the Frankfurt area fell kindly for Lica, and although his shot was parried by Trapp, Ghilas followed up to turn the ball home at the far post.