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 but Porto's numerical advantage lasted only a few moments as Wendell and Agustin Marchesin were both sent off in the span of five minutes.
Substitute Angel Correa clinched the win for Atletico with a cool finish on the counter-attack in the 90th minute, while Porto then earned a consolation penalty which Sergio Oliveira converted with the final kick of the game.
Diego Simeone's Atleti side began the game bottom of Group B but ended it second on seven points, Porto coming third with five and qualifying for the Europa League.
"I'm happy, proud of my players," Simeone said. "They showed personality, experience, they knew how to suffer, with the difficulties we faced. We were missing three centre-backs out of four but there are players who haven't played many minutes who are important.
"We had one of the most complicated groups. I thought we'd get points against everyone, but we couldn't against Liverpool. We got more points than Milan and Porto. We need to improve, but this is the way.
"It's a great day. We'll remember it as a classic Champions League game against a great team. The game had everything."
Atleti had suffered a shock 2-1 defeat at home to 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.
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 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."