Real's travails looked like continuing with Aleksandar Kolarov having given the visitors the lead after Marcelo had cancelled out Edin Dzeko's opener. But first Karim Benzema equalized with a quality strike before Ronaldo's dramatic winner that owed much to an error from City goalkeeper Joe Hart.
While Real created the majority of the chances, the result will be a cruel blow to City as they look to progress past the Champions League group stage for the first time.
While Manchester City manager Roberto Mancini spoke before kickoff of his intentions to attack Real, there was little surprise that his formation suggested something different. City lined up with Javi Garcia and Gareth Barry as midfield shields in front of the back four, with Yaya Toure providing extra cover in a slightly advanced role. David Silva and Samir Nasri were kept narrow in support of lone front man Carlos Tevez. When Nasri went off inside the opening period, the formation took on an even more defensive look when Aleksander Kolarov came on as his replacement.
That safety-first approach did little to stem the tide of Real attacks in the early stages, though. City's defense was twice exposed in the opening 15 minutes as Ronaldo capitalized on the suspect positioning of right-back Maicon.
First Gonzalo Higuain found the Portuguese forward down the left and one-on-one with Vincent Kompany he shifted the ball inside the Belgian and aimed a low side-footed effort toward the far post that Hart did well to tip wide.
Indeed, City owed much to Hart in keeping the scores level. Just a few minutes later the City goalkeeper saved well diving to his left as Higuain got his head on the ball to change the direction of a Ronaldo shot right in front of goal.
The opportunities kept coming for Real. Angel Di Maria was causing problems coming inside from the right flank to deliver testing through balls over the top. The first caught out City's defense far too easily and put Higuain in behind, but the Argentinian striker failed to control the ball as he would have liked, allowing Hart to rush out and get a block on the ball. From the rebound, Higuain played it back to Ronaldo on the edge of the area, whose deflected effort was put over the top by Sami Khedira at the far post.
Di Maria threatened again toward the end of the half as a similar through ball saw Higuain stretch to get a foot on the ball just ahead of Kompany, but the forward couldn't keep his shot down.
City created very little going forward in the opening 45 minutes, there best openings coming courtesy of typically Herculean attacking bursts by Toure. On both occasions, though, the former Barcelona man got his final ball wrong-first to Carlos Tevez and then forcing David Silva to check his run.
Real Madrid continued the pressure in the second half as Marcelo twice had City hearts in mouth and Hart at full stretch, though with efforts from outside the box, the first of which whistled wide of the post and the second rising just over the crossbar.
But it was City, who went in front with an archetypal sucker punch in the 68th minute. Toure showed tremendous strength once more to hold onto the ball on the edge of his own box embarking on an unstoppable burst down field. Unlike in the first half, this time Toure produced a fitting final ball as he slipped in substitute Dzeko, who showed brilliant composure to slot a shot left-footed past Casillas.
Minutes later it could have gotten even worse for Real. Toure got forward once more but this time fired wide into the side netting at the near post, when perhaps he should have done better.
City were made to pay almost immediately. Marcelo again found space going forward, this time turned inside on his right foot and struck a shot that took a deflection off Javi Garcia that was enough to loop it over the dive of Hart and into the net.
The game had now firmly sprung into life. Going almost straight down the other end, Tevez crossed low into the box and substitute Pablo Zabaleta could only send the ball wide of the far post.
While City were now more of a forward threat, it was Real that remained the side in control. Karim Benzema fired an effort across goal from a narrow angle that Hart was forced to parry away.
But it was City who looked like they had grabbed a vital winner. Always a threat from set pieces, Kolarov sent a wicked in-swinging freekick across goal from the right that evaded everyone to the surprise of Casillas who reacted too late to prevent it from sneaking inside his far post.
Just as City thought they had pulled off one of the most memorable results in their history Real produced another quick-fire response. Di Maria, drifting inside once more, played the ball into Benzema on the edge of the box and with two superb touches the France international turned his marker and produced an instant shot that beat Hart low at his near post.
But just moments after failing to make the most of a chance, Ronaldo grabbed the decisive fifth goal of an enthralling second half. Ronaldo turned inside from the left and sent a shot goalward that dipped and bounced right in front of Hart and somehow found the back of the net. It wasn't the easiest of shots to deal with but the England goalkeeper will be bitterly disappointed to have conceded.