Rayo missed several opportunities to take the lead before Ronaldo's brilliant effort secured the 1-0 win for the league leaders.
Piti struck the post and Michu was denied by a last-ditch tackle by Sergio Ramos in the first half but all home side's good work was undone when they failed to properly clear Mesut Ozil's corner.
The ball fell to Ronaldo in an awkard position but he still managed to skilfully backheel it in from 10 yards out.
"Rayo played a great game and made things very difficult for us," Ronaldo told the club's website.
"They had chances to draw the match, but we defended well and earned a favourable score."
"It was a messy game, especially in the opening minutes, because neither team had control of it. The coach spoke to us during half-time and we concentrated harder.
"We're very proud of the job we did because these three points will help us win the title.
"I didn't even think when I scored. I just hit the ball with my heel and therefore surprised the defenders and the goalkeeper.
"I hit it strongly enough so they couldn't stop it. It gave us the victory, so it's worth twice us much to us."
Madrid remain 10 points clear of defending champions Barcelona who are in second.