The 32-year-old Bale, whose contract expires at the end of this month, joined Madrid from Tottenham in 2013. He played on loan with the English club in 2020-21.
"I arrived here nine years ago as a young man who wanted to realize my dream of playing for Real Madrid. To wear the pristine white kit, to wear the crest on my chest, to play at the Santiago Bernabéu, to win titles and to be part of what it's so famous for, to win the Champions League. I can now look back, reflect and say with honesty that this dream became a reality and much, much more," he said in a letter posted on Twitter.
"To be a part of this club's history and to achieve what we achieved while I was a Real Madrid player, has been an incredible experience and one I will never forget," the Wales forward said.
He helped Madrid win five European titles, scoring in the 2014 and 2018 Champions League finals. He also helped the club lift four Club World Cup trophies and win three Spanish league titles.
Bale, whose next move remains unknown, had an old feud with Madrid fans but was thankful to everyone at the club in his farewell letter.
"I write this message to say thank you to all my teammates, past and present, my managers, the back room staff and to the fans that supported me," he said.
Bale was booed the few times he got to play this season, and was even jeered by some during the team's title celebrations after it won the Champions League a record-extending 14th time last week. He has long been at odds with Madrid fans who felt he didn't make the club a priority and instead focused on Wales' national team.
Fans and Spanish media said Bale failed to consistently perform to his potential since joining for a then-world record fee of 100 million euros (now USD 107 million).
Earlier this year, Bale spoke out against the psychological harm that professional athletes can receive from the media after a leading Spanish sports daily published a column entitled "The Welsh Parasite." Bale accused the media of "encouraging anger and disappointment in fans."
This season, he made only seven appearances and scored one goal with the club.
"Together we were able to create some moments that will live forever in the history of this club and football," Bale said.
"It has been an honour."