Portugal will now face European champions Spain, who earlier advanced thanks to a comfortable 3-0 win over Austria.
"I've been through moments like these and I said that today we had to know how to suffer," said Portugal superstar Cristiano Ronaldo. "To win a tournament like this, you have to know how to suffer."
Croatia hit the target first as veteran Ivan Perisic smashed the opener into the net in the 53rd minute.
But Cristiano Ronaldo converted a penalty in the 68th to score his first goal in a World Cup knockout match and equalise for Portugal. At the age of 41, he also became the oldest player to score in a World Cup knockout stage.
Croatia had two goals ruled out in the second half, one from Nikola Vlasic in the 56th and another from Petar Sucic in the 80th. Ronaldo also had a goal disallowed shortly before he scored from the spot.
The game looked set to go to extra-time, but Ramos became Portugal's hero.
His goal seemed to have put an end to the match and secured Portugal in the last 16, only for more drama to come as Croatia found the back of the net in the 12th minute of stoppage time.
A ball was crossed and Croatian fans went wild when Josko Gvardiol struck it into the net. However, after video review, the referee ruled the goal out for a controversial offside in the play.
Mario Pasalic was onside when the ball was crossed to the box, but the referee deemed Matanovic had touched it, which changed Pasalic situation when he passed the ball to Gvardiol.
Bottles rained down onto the pitch from the Croatia supporters and the match was briefly interrupted, but the decision stood and Croatia were out of the Cup.
"They scored, it got a bit stressing for us, but there was relief after the penalty. From that point, the game got easier, things were still a bit difficult, but that's what the competition is and we have to be prepared and keep going," Ronaldo said.
But it was another heartbreak for Luka Modric and his teammates as the goal was also disallowed for offside.
After the final whistle, Portugal players held up the No.21 shirt - Diogo Jota's - which was then worn by Ronaldo, who pointed to the sky with tear in his eyes.
Jota and his brother, Andre, died on July 3, 2025 in a car accident.
"It's a special day, as you know, for our Jota. He's up there illuminating us, and it's a special moment. We all feel he's with us, and it only made sense to win to honour him in the best way," Ronaldo said.