The 29-year-old Ramos had been linked to interest from Manchester United throughout the offseason.
"As I said at my presentation 10 years ago, forming part of this cub is a dream and I want to keep living it as long as possible," Ramos said at a news conference attended by new coach Rafa Benitez.
Neither club nor player revealed the financial details of the contract, but Ramos said he received offers of more money from other clubs.
"I know that these negotiations have been drawn out . but my heart has always been here," Ramos said. "If this had been about money I wouldn't have stayed."
Since arriving from Sevilla in 2005, Ramos has helped Madrid win one Champions League trophy, three Spanish league titles and two Copa del Rey titles. His injury-time goal in the final of the 2014 Champions League forced extra time, and Madrid ended up beating crosstown rival Atletico Madrid 4-1 for its record 10th European Cup.
Ramos also formed part of Spain's 2010 World Cup-winning team and its two European champion squads.
Madrid opens the Spanish league season at promoted club Sporting Gijon on Sunday.