Gareth Bale scored his first European goal since December 2014 to give the hosts the lead in the 16th minute with a curled left-foot drive and an own goal by Legia's Tomasz Jodlowiec, deflecting in Marcelo's shot, doubled their advantage on Tuesday.
Miroslav Radovic pulled one back for the Poles from the penalty spot but Marco Asensio made it 3-1 in the 37th minute with a low shot on his Champions League debut.
Substitute Lucas Vazquez volleyed home a cross in the 68th minute and former Juventus striker Alvaro Morata rounded off the win by converting a pass from Cristiano Ronaldo, who is still waiting for his 100th goal in UEFA competitions, in the 84th.
There was concern in Madrid before the game about the threat that would be posed by visiting fans after violence marred Legia's game against Dortmund in September.
There was trouble outside the Santiago Bernabeu stadium before kickoff on Tuesday, with five supporters and two police officers suffering minor injuries, according to local media reports.
On the pitch, Legia nearly caused the holders an early surprise with Jodlowiec shooting straight at Keylor Navas and midfielder Vadis Odjidja hitting the post.
Twenty-year-old winger Marco Asensio built on his impressive start to his Real career by netting his fourth goal of the season with a confident strike after being teed up by Ronaldo.
Ronaldo, the leading marksman in Champions League history, turned provider again by releasing substitute Morata for the fifth goal after Vazquez had lashed home the fourth.
The prolific Portuguese narrowly missed out on a century of goals in UEFA competitions by striking wide of the far post.