Ajayi had put the Millers ahead with 20 minutes remaining, but Bright Osayi-Samuel’s 85th-minute equaliser appeared to have rescued a point for struggling Rangers.
However, Ajayi rose to meet Anthony Forde’s left-wing free-kick with seconds remaining.
It was the Nigerian’s fifth goal in four games and took third-from-bottom Rotherham level on points with Wigan and Reading.
A poor game finally came to life when Ajayi opened the scoring in emphatic fashion. Joe Newell’s shot was blocked by defender Toni Leistner and Ajayi pounced on the loose ball and rifled a right-footed shot beyond goalkeeper Joe Lumley and into the far corner of the net.
Hosts Rangers hit back when Jake Bidwell played the ball down the left to substitute Pawel Wszolek who cut it back for Osayi-Samuel to score from close range.
QPR had created the majority of the few chances the game produced, Tomer Hemed spurning two of them. Goalkeeper Marek Rodak denied Hemed at point-blank range and Hemed was then guilty of a bad miss when he headed over from a right-wing free-kick.
But Rangers were made to pay for their profligacy with Ajayi pouncing five minutes into time added on to give his side three priceless points in the battle for survival.
QPR: Lumley, Furlong, Leistner, Lynch, Bidwell, Samuel, Cousins (Shodipo 72), Luongo, Freeman, Eze (Wszolek 80), Hemed (Wells 72). Unused substitutes: Ingram, Cameron, Scowen, Manning.
Rotherham: Rodak, Vyner, Ihiekwe, Wood, Mattock, Ajayi, Taylor, Towell (Crooks 64), Wiles (Jones 90), Newell (Forde 83), Smith. Unused substitutes: Palmer, Price, Yates, Williams.
Referee: D Webb (County Durham).