The Primeira Liga runners-up of 2016-17 went into the game in third place in Group G, one point behind their opponents, and they took an early lead when Hector Herrera scored from close range after 13 minutes.
Leipzig edged the first encounter between the two teams 3-2 and they confidently battled their way back into the game before Timo Werner, on as a second-half substitute, scored his first Champions League goal to level the scores early in the second half.
But Danilo's diving header and Maxi Pereira's stoppage-time strike killed off Leipzig's resistance and Porto emerged as worthy winners, leaving the German side with the daunting prospect of needing to take points from both their remaining games – away at Monaco and at home to runaway group leaders Besiktas.
Porto's task is similarly difficult, but they will go into their next game at Besiktas knowing a victory could make sure of their place in the last 16.
Home forward Moussa Marega went down clutching his hamstring following an early challenge and after 12 minutes, having sprinted to win Porto a corner, he limped out of the game.
Moments later Porto took the lead when a deep corner was headed back into the box and Danilo poked the ball out of the melee to Herrera, whose low shot found the bottom right corner of the net despite Peter Gulacsi getting a hand to it.
Vincent Aboubakar almost doubled their lead four minutes later when he chased down a loose ball and forced a desperate save from Gulacsi with a close-range shot.
At the other end, Jose Sa made a fingertip save in the top corner from Bruma's audacious 25-yard free-kick, before Leipzig's Emil Forsberg had a tap-in ruled out for offside against Jean-Kevin Augustin.
Within three minutes of the second half, Marcel Sabitzer split the Porto defence with a bouncing pass that Werner latched onto and took a touch before launching the ball high over Sa and into the far corner of the net.
The goal lit a fire under Porto's attack but they struggled to penetrate a packed Leipzig defence until Danilo headed narrowly over the crossbar after an hour.
One minute later Danilo was presented with another chance when Alex Telles floated a long free-kick into the penalty area, and the Guinea-Bissau-born midfielder scored with a diving header into the right corner of the net.
Sabitzer flashed a shot across the face of Sa's goal after good work by Forsberg but Porto absorbed what little pressure Leipzig applied in the closing stages and, aside from the early injury to Marega, coach Sergio Conceicao was given little to complain about.
Maxi Pereira added some late gloss to the win when he raced clear of Leipzig's defence in stoppage time after Aboubakar did well to put him through, the 33-year-old making no mistake with the finish, slotting the ball low into the corner of the net to seal an important victory.