"Real Madrid and FC Porto have agreed the transfer of Iker Casillas to the Portuguese club," Real Madrid said in a statement on Saturday after three days of intense negotiations.
At 34 years Casillas leaves a club with which he grew up, joining Real's youth academy aged nine in 1990 and winning 19 trophies during his 16 seasons as a professional.
"One of the best goalkeepers in this club is not just leaving, today the greatest goalkeeper in the history of the club and in the history of Spanish football is moving on to a new stage in his footballing career," the statement said.
Casillas had two years left on his contract, but the highly-anticipated transfer is seen as an historic turning point for the club who are reported to be trying to recruit Manchester United's 24-year-old Spanish goalkeeper David de Gea in a bid to rejuvenate after finishing second in La Liga last season behind Barcelona, who also won the Champions League title.
"Iker is leaving, but his legacy is here forever. His attitude and actions in the 725 games he played in the our jersey shine a light on the path for those who dream of being part of this team," the statement said.
"For Real Madrid today is a day, above all, of thanks and recognition .... This goodbye evokes thousands of sensations and memories charged with hope, anticipation, sacrifice, strength and unique triumph," the club said.
In 2010 Casillas made front pages worldwide in a photograph of him raising the World Cup in South Africa after leading Spain to the title for the first time.
But last summer the image was of a dejected Casillas on his knees as a rout by the Netherlands helped dump Spain out of the World Cup in Brazil.
With Madrid, Casillas had enchanted the Bernabeu Stadium for years with his catlike reflexes, gravity-defying saves and fearless one-on-ones, earning the nickname "Saint Iker" from fans.
A World Cup winner and two-time European champion with Spain, Casillas had clinched five Spanish titles, three Champions League trophies and two King's Cup crowns since making his first-team debut with Real Madrid in 1999.
Casillas had fallen out of favour during Jose Mourinho's reign at Real but returned to the side after the Portuguese left for Chelsea, winning the Champions League for a third time in 2014.
"Thank you Iker, for everything you have done. Thank you for being a symbol of the best in our history. You are going but we will never forget that you'll always belong to the heart of Real Madrid," the club statement added.
Porto, coached by former Real Madrid and Spain goalkeeper Julen Lopetegui, will compete in next season's Champions League after finishing runners-up to Benfica in the 2014-15 Portuguese season.
Lopetugui already has six Spaniards in his squad and both his assistant coaches are Spanish.