Lopetegui had a brief stint at Real during his playing career and coached their B team in 2008.
The search to replace Zinedine Zidane, who left after guiding Real to a third-straight Champions League title, lasted nearly two weeks, with Mauricio Pochettino, Antonio Conte and Jurgen Klopp among the names reportedly in the frame.
Following the tournament, the 51-year-old will officially sign his three-year contract at Real.More news: U.S. attorney general curbs asylum for immigrant victims of violence
If Spain fail to impress at the World Cup, then Lopetegui will head to Real Madrid under a cloud that will make an already difficult job even harder.
Spain's senior side are unbeaten under Lopetegui, having won 14 and drawn six of their 20 matches and begin their World Cup campaign against Portugal on Friday.
Lopetegui is now preparing to lead Spain at the World Cup but the 51-year-old will depart his role as national team coach at the end of the tournament.
Zidane's resignation came as a big shock, as he had just led Real Madrid to a third consecutive UEFA Champions League triumph.
But Los Blancos struggled domestically, managing only a third-place finish and ending up 17 points behind champions Barcelona.