Cristiano Ronaldo stared at the floor at the final whistle in disbelief.

The Real Madrid forward had endured one of those nights when it was hard to believe we were watching the world's most expensive footballer. Ronaldo squandered two wonderful second-half chances that looked like being costly when Nicklas Bendtner scored his second to haul Denmark level but Silvestre Varela, a late substitute, struck three minutes from time to spare his captain considerable embarrassment and give Portugal a crucial win.

It was a dramatic end to a compelling game and revived Portugal's hopes of reaching the quarter-finals, and left Denmark cursing their failure to see out the match after working so hard to recover from being two-nil down in the first half. Morten Olsen's side enjoyed far more possession than Portugal — they made 200 passes more than their opponents — but they looked vulnerable defensively throughout and it was no surprise when Portugal punished them for a third time through Varela.

It was a poor goal to concede from Denmark's point of view. Fábio Coentrão crossed from the left and Simon Poulsen was too slow to close down Varela, who thrashed a right-footed shot beyond Stephan Andersen after initially completely mis-kicking with his left. Denmark, remarkably, could still have salvaged a point but Lasse Schone, another late substitute, shot high and wide after breaking clear on the right-hand side of the penalty area.

Ronaldo stayed on the field to applaud the Portugal fans at the end, unlike the Germany game when he could not get off the pitch quickly enough, but this was a humiliating evening for the 27-year-old and it was impossible not to question the merits of him wearing the captain's armband. As well as missing two gilt-edged opportunities, Ronaldo spent much of the game berating his team-mates whenever the ball was given away — there were less than two minutes on the clock when he issued his first ticking off – and he picked up a yellow card in the closing seconds for a reckless challenge that was symptomatic of his evening. He looks a different player in a Portugal shirt and has now appeared 21 times for his country at major finals and scored only five goals.

Bendtner was also entitled to feel frustrated, although for entirely different reasons. The Arsenal striker has often failed to deliver on the big stage but no one could level that accusation against him here. He marked his 50th cap with his 19th and 20th goals for Denmark and could count himself highly unfortunate to end up on the losing side. Portugal will be keen to see the back of Bendtner. He has scored six goals in five appearances against them.

Denmark had made the better start but they lost their way after the first 10 minutes and fell behind to a well-worked set-piece routine. João Moutinho's inswinging corner was directed towards the near post and Pepe timed his run perfectly, losing his marker Daniel Agger before heading powerfully inside the upright. A second Portugal goal followed 12 minutes later when more suspect Danish defending was punished.

Poulsen's header from Coentrão's deep centre lacked conviction and dropped at the feet of João Pereira, who fed Nani on the right flank. With Denmark slow to react, the Manchester United winger had the time and space to look up and slide a simple ball towards the six yard-box where Helder Postiga, who had spent much of the first half on the receiving end of rollickings from Ronaldo, got in front of Simon Kjaer and rammed into the roof of the net to become only the sixth player to score in three different European Championships. Not bad for a striker that was considered such a flop at Tottenham Hotspur.

Portugal were cruising at that point but Bendtner's first goal, in the 41st minute, dragged Denmark back into the game. Jakob Poulsen, an early substitute for the injured Niki Zimling, whipped a cross to the far post where Michael Krohn-Dehli headed back across goal for Bendtner to nod home.

Then came the first of Ronaldo's two incredible misses. Nani drilled a diagonal ball from right to left that Postiga astutely allowed to run into the path of Ronaldo. The former Manchester United winger homed in on goal and there was a sense of inevitably that the net would bellow moments later but Andersen rushed from his line and saved.

If that moment was difficult to reconcile with a player who scored 60 goals for Real Madrid last season, it was nothing compared to the head-shaking induced by his wastefulness in the 78th minute. Nani was once again the creator, playing an incisive first-time pass that released Ronaldo, who had nobody close to him as he bore down on the Denmark goal. Yet, inexplicably, Ronaldo shot horribly wide, his wayward effort not even close to the upright and prompting whistles from the massed ranks of Ukrainian supporters behind the goal.

Two minutes later Bendtner completed Denmark's comeback, heading in Christian Eriksen's centre at the far post after Pepe was caught ball-watching. Ronaldo raced to get the ball and place it on the centre spot, and he must have felt an overwhelming sense of relief seven minutes later when Varela, who was guilty of a poor miss towards the end of the Germany game after coming on as a substitute, rifled home to the delight of the Portugal supporters.