Spain ended up topping Group B by virtue of goals scored after an injury-time strike by Iago Aspas, confirmed by the video assistant referee, rescued a 2-2 draw against Morocco in Kaliningrad and will take on the host nation in Moscow on Sunday.
Portugal, who finished level with Spain on five points, conceded a late penalty as Iran drew level at 1-1 and faced a heart-stopping moment as Mehdi Taremi put a shot inches wide, a goal that would have sent the European champions crashing out.
Ronaldo was at the heart of a series of VAR incidents in Saransk. First he had a penalty saved by Ali Beiranvand and was later saved by a review as Iran's players called for him to be sent off following an incident with Morteza Pouraliganji.
Furious Iran coach Carlos Queiroz said Portugal captain Ronaldo, who was booked, should have been sent off for using an elbow and lashed out against VAR, saying it was "not going well".
"I don't want to talk too much, it's about my country and a player. I know it'll be a war against me," said Queiroz, who coached his native Portugal at the 2010 finals in South Africa.
Top spot up for grabs for Spain, Portugal
He continued, "But the reality is you stopped the game for VAR, there is an elbow," he added. "An elbow is a red card in the rules. In the rules it doesn't say if it is (Lionel) Messi or Ronaldo."
Portugal will face Uruguay in the last 16 in Sochi on Saturday.
Relieved Spain boss Fernando Hierro, who took over as coach on the eve of the World Cup, said he knew the group was going to be tough, acknowledging his side had to tighten up.
"The first conclusion I draw is that despite all these challenges we ended on top. I'm looking at the silver lining," he said.
"Obviously we can improve. Five goals in three matches is not the way forward. The players are professional, they understand what the situation."
There have now been 19 penalties taken at the 2018 World Cup,more than in any other edition of the competition.
Earlier, 10-man Russia were handed a World Cup reality check, losing 3-0 to Uruguay as the South Americans topped Group A with a perfect record.
Spain stand behind under-pressure De Gea
The host nation rode a wave of national euphoria in their first two matches, sweeping aside Saudi Arabia and Egypt despite being initially written off as no-hopers but could not keep the magic alive against the South Americans.
An early free-kick from Barcelona's Luis Suarez, an own goal from Denis Cheryshev and a late strike from Edinson Cavani sealed top spot for Uruguay, with Russia through to the knockout stages as runners-up.
"Obviously, the first few matches you're tense and don't want to make any mistakes or lose," said man-of the-match Suarez."
He added, "One mistake can be very expensive. Today we came to the pitch with a different attitude and perspective and that's made the pressure better. That's what we saw on the pitch."
Disappointed Russian coach Stanislav Cherchesov said it was better to lose heavily now than in future games, adding his players had shown nerves for the first time but were psychologically prepared for the knockout stages.
"The outcome is very simple. We did what we had to do," he said. "But you cannot start a game of this level with such a mistake, we gave up a goal.
"Uruguay then started playing their type of football. And then we got a red card," he added.
In the other match in Group A, Saudi Arabia beat Mohamed Salah's Egypt 2-1 in a matchup of two eliminated sides in Volgograd but the main talking point was Egypt goalkeeper Essam el-Hadary becoming the oldest player ever to appear in a World Cup.
El Hadary, 45 years and 161 days old, smashed the record set by Colombia goalkeeper Faryd Mondragon, who was aged 43 years and three days when he played against Japan in Brazil in 2014.
The veteran goalkeeper produced a stunning save from a Fahad al-Muwallad penalty after Salah had given Egypt the lead with his second World Cup goal, but could not keep out another spot kick by Salman al-Faraj in first-half injury time.
COMMENTS
Comments are moderated and generally will be posted if they are on-topic and not abusive.
For more information, please see our Comments FAQ