Antoine Griezmann put Atletico in front against the run of play in the 56th minute, bundling the ball into the net following a corner after the hosts had missed a glut of clear chances either side of the break.
Atletico's lead was threatened when Yannick Carrasco was sent off in the 67th minute for raising his hands to Otavio as tempers raged following a foul.
However, Porto's numerical advantage lasted only a few moments as Wendell was dismissed for appearing to lightly strike Matheus Cunha with his elbow, sparking a melee involving the players and coaching staff.
Diego Simeone's team saw out the game in typical fashion, ceding Porto the ball but closing down the spaces, and substitute Angel Correa clinched the win with a clinical finish on the counter-attack in the 90th minute.
Rodrigo de Paul then added a third goal 60 seconds later as Porto ran out of energy and hope.
The home side earned a consolation penalty which Sergio Oliveira converted with the final kick of the game.
Atleti began the game bottom of Group B but ended it second on seven points, with Porto coming third with five and qualifying for the Europa League.
"This match and this group stage campaign sums up just what Atleti is all about," said Griezmann.
"We are a true group and we need everyone to do their bit to get to where we want to. We never stop believing."
Liverpool finished top with 18 points after winning 2-1 at AC Milan, who came bottom with four.
Atletico had the odds stacked against them after losing their previous game at home to Milan 1-0 and to progress they needed not only to win but also for Liverpool to do them a favour.
Atleti suffered a shock 2-1 defeat at home to Real Mallorca in LaLiga at the weekend and were missing their top three central defenders in Jose Gimenez, Stefan Savic and Felipe, forcing midfielder Geoffrey Kondogbia and right back Sime Vrsaljko to play in the heart of defence.
Things got off to an ominous start when striker Luis Suarez was forced off injured in the 13th minute but, as so often in recent years, Atletico came out fighting.
"We knew exactly what was at stake but we're Atletico Madrid, we do things the hard way and it tastes much better like this," said defender Mario Hermoso.
Atleti created their first real chance when Carrasco carved his way through the defence but Marcos Llorente fired his ricocheted cross against the feet of goalkeeper Diogo Costa.
Porto dominated the rest of the first half and Atletico keeper Jan Oblak produced an outstanding save to deny Luis Diaz, while Llorente threw himself across goal to prevent Mehdi Taremi reaching the loose ball.
Taremi missed two clear chances early in the second half, blazing over the bar and then being denied by the feet of Oblak, before Griezmann gave Atleti an unlikely lead with a scrappy goal.
Porto barely recovered from the shock of going behind and substitute keeper Agustin Marchesin was sent off from the stands as both sides struggled to control their tempers.
"It's been an incredible night," said Oblak. "I'm sure many people didn't believe in us but it's in matches like these where we show what a team we really are."