Having been unfairly booked for diving, the Portugal star headed home the opener before James Rodriguez made the points safe
La Liga leaders Barcelona eased to a 4-0 win over struggling Almeria earlier on Wednesday and Real endured a far more testing encounter against their tireless near-neighbours.
Rayo attacked with verve and intelligence as they enjoyed the better of the first half and, although Real improved after the interval, the prolific form showcased during Sunday's 9-1 demolition of Granada eluded them.
Ronaldo netted a remarkable five times in that game, but his solitary 68th-minute header here was arguably of greater importance in terms of the title race.
The Ballon d'Or holder turned provider for the impressive James Rodriguez to make the points safe with 17 minutes left, ensuring Madrid remain four points behind Barca and five ahead of reigning champions Atletico Madrid in third.
James crossed for Ronaldo to volley wide in the seventh minute before the Colombia playmaker was thwarted by David Cobeno, who started in goal for Rayo after Cristian Alvarez was injured during the warm-up.
In between those two chances, Gael Kakuta sent a 25-yard effort whistling narrowly off target and Iker Casillas did well to keep out a low strike from Alberto Bueno as Paco Jemez's men played a full part in an entertaining opening period.
Gareth Bale netted a controlled volley in the 23rd minute but was correctly ruled to be offside on the end of Luka Modric's lofted throughball.
It was a rare moment of encouragement during the first half for Real as Rayo, buoyed by a run of four wins in their previous five matches, dominated possession and pressed to stifle Real's creativity.
Manucho almost turned a 34th-minute corner past Casillas and the Spain goalkeeper made an even better reaction save from Roberto Trashorras' back-post header.
Madrid boss Carlo Ancelotti had plenty to ponder at the break as Rayo were rapturously applauded from the field.
The visitors began the second half in more encouraging fashion, as Bale sent a diving header agonisingly wide from James' searching cross.
Ronaldo was booked for simulation when he had penalty claims rejected before the Portugal star combined with Bale for Karim Benzema to pull a shot past the far post in the 53rd minute.
Marcelo and Bale hit rasping drives just wide as Real cranked up the pressure further, with James showcasing some dazzling footwork before crashing a left-footed shot over with 65 minutes played.
Finally, the pressure told when full-back Dani Carvajal raced down the right flank and Ronaldo stooped to head home his cross.
Rayo surged forward in response as Nacho stung Casillas' gloves from 20 yards but James got the goal his efforts deserved, slotting into the bottom corner with 17 minutes to play.
Casillas added a further flourish to a fine evening's work when Manucho headed towards the bottom corner and Rayo full-back Tito lashed a shot wastefully wide, meaning Ancelotti's side gladly avoided a grandstand finish.