Real had looked certain to make the last four following last week's 3-0 first-leg victory in Turin, but Juve staged a sensational comeback in the return at the Santiago Bernabeu.
Mario Mandzukic's two first-half headers kept the dream alive before Blaise Matuidi capitalised on a Keylor Navas error to pull the Italians level with half an hour left.
But, just as a thrilling second leg looked to be heading for extra time, English referee Michael Oliver awarded Real a penalty in stoppage time.
With the game in the 97th minute, Ronaldo made no mistake from 12 yards, ensuring a 3-1 defeat on the night but a 4-3 win on aggregate.
Gianluigi Buffon, making his 125th and perhaps final Champions League appearance before retirement, was sent off for his protests. If it is to be the 40-year-old's European farewell, it was a sad way to end.
On Wednesday morning, Madrid newspaper Marca had revelled in Barcelona's collapse against Roma the night before with the headline 'Total Failure', but Zinedine Zidane's side came within a whisker of trumping that with an emphatic loss on their own patch.
Real conceded the opener after just 76 seconds, the fastest they have ever conceded at home in the Champions League.
But Ronaldo came up trumps again, scoring in his 11th consecutive fixture in this competition, extending his own record, and keeping Real on course for a third consecutive European triumph.
Zidane had handed Gareth Bale his first Champions League start since September but this was far from a night of redemption for the Welshman. He was hauled off at half-time, with Real two down, and replaced by Marco Asensio.
Edin Dzeko needed six minutes to set Roma on their way against Barcelona, but Juve needed just over one.
Sami Khedira hung a deep cross to the back post where Mandzukic powered a header past Navas.
His second, eight minutes before half-time, was almost a carbon copy. This time it was substitute Stephan Lichtsteiner, on for Mattia De Sciglio, who was given too much space by Marcelo to cross. Again Mandzukic jumped higher than Dani Carvajal as he nodded between Navas' right hand and the near post.
In between, Real had dictated possession and created chances of their own, but Buffon was in imperious form. Just after Mandzukic got his second goal, Raphael Varane rattled the crossbar with a header to which Buffon got his fingertips.
Real, though, were shaky at the back, the channel between Marcelo and 21-year-old Jesus Vallejo regularly offering Juve hope.
Bale's night was up at half-time as he and Casemiro were replaced by Zidane's tried-and-trusted duo in Europe of Vazquez and Asensio.
However, Real's defensive problems were exposed again on the hour.
Douglas Costa crossed from the right but Navas dropped the ball, allowing Matuidi to stab home and level the tie.
Isco drove towards the bottom corner but Buffon's hand was there and the game looked to be drifting towards extra-time until the 93rd minute.
Hanging in the air at the back post, Ronaldo nodded down to Vazquez who looked certain to score from five yards before Medhi Benatia desperately challenged him from behind.
Oliver pointed to the spot much and in the ensuing chaos, Buffon was sent off for his protests.
Wojciech Szczesny came on to face the decisive strike of the game. Ronaldo exhaled and slammed it into the top right-hand corner to take Real into the semi-finals.