Ronaldo's exploits overshadowed a hat trick by Karim Benzema at the Santiago Bernabeu Stadium.
It was the second straight game in which Benzema scored at least two goals after a brace in the team's 4-1 win over Getafe in the Spanish league on Saturday. Those were his first goals since being implicated in a blackmail case in relation to an alleged extortion scam over a sex tape featuring his French national teammate Mathieu Valbuena.
Mateo Kovacic also scored for Madrid.
Also, Zlatan Ibrahimovic and Lucas set each other up for goals as Paris Saint-Germain beat Shakhtar Donetsk 2-0.
PSG was already guaranteed to finish second behind Madrid.
Madrid advanced with 16 points and PSG with 13. Shakhtar finished third with three points and dropped into the Europa League while Malmo came last, also with three points but with only one goal scored and 21 conceded.
Central defender Naldo scored a brace as Wolfsburg advanced to the knockout stage for the first time when it beat three-time winner Manchester United 3-2. United dropped to third place in the group when PSV Eindhoven came from behind to beat CSKA Moscow 2-1 and join Wolfsburg in the round of 16.
Wolfsburg won the group with 12 points and PSV finished with 10, two more than United, which will now have to play in the Europa League. CSKA finished last with four points.
PSV advanced for the first time since 2006. Veteran defender Sergei Ignashevic converted a 76th-minute penalty to give CSKA the lead, but PSV captain Luuk de Jong equalized just two minutes later, and midfielder Davy Proepper's low shot sealed the win in the 86th.
In Wolfsburg, Anthony Martial put United ahead in the 10th, but Naldo volleyed in the equalizer in the 14th. Vieirinha regained the lead for Wolfsburg in the 29th, before an own goal by Joshua Guilavogui in the 82nd tied the score. But two minutes later Naldo scored his second with a header to seal Wolfsburg's victory.
Luciano Vietto scored one goal and set up another as Atletico Madrid won 2-1 at Benfica to win the group.
Playing at the same stadium where it lost the 2014 final, Diego Simeone's side avenged a 2-1 loss to Benfica from earlier this season.
Vietto passed for midfielder Saul Niguez to convert Atletico's aggressive start into a goal after half an hour, before he added a strike of his own early in the second half.
Benfica substitute Kostas Mitroglou halved the difference in the 75th.
Galatasaray was held to 1-1 at home by Astana in the other Group C match. The Turkish side finished third and will join the Europa League.
Atletico finished with 13 points, Benfica with 10, Galatasaray with five and Astana with four.
Raheem Sterling netted in the 80th and 81st minutes and Wilfried Bony scored in the 85th to spark Manchester City's late turnaround in a 4-2 win over Borussia Moenchengladbach
To qualify in first place, City also needed Juventus to lose in Sevilla and last season's runner-up was beaten 1-0.
Sevilla secured a berth in the Europa League as Fernando Llorente scored against his former team.
On the two previous occasions City reached the knockout stage, it was runner-up in the group and drew Barcelona both times. The English club is likely to get an easier draw in the last 16 this time.
City finished with 12 points, Juventus with 11, Sevilla with six and 'Gladbach with five.