Federer, unable to play because of injury, was part of Wimbledon's celebrations for the centenary of Centre Court in July
The 20-time Grand Slam champion has not played since Wimbledon 2021, after which he had a third knee operation.
"My body's message to me lately has been clear," Swiss Federer, 41, said.
"I have played more than 1,500 matches over 24 years. Now I must recognise when it is time to end my competitive career."
He added: "To the game of tennis, I love you and will never leave you."
Federer has struggled with a knee problem for the past three years that has restricted him to only three of the 11 Grand Slams staged since the start of 2020.
He has not played a competitive match since losing to Hubert Hurkacz in the quarter-finals at Wimbledon last summer.
He subsequently announced he needed more surgery on his knee having previously had two operations in 2020.
Only Spain's Rafael Nadal, with 22 majors, and Serbia's Novak Djokovic, with 21, have more men's Grand Slam singles titles than Federer.
"This is a bittersweet decision because I will miss everything the tour has given me," added Federer.
"But, at the same time, there is so much to celebrate. I consider myself one of the most fortunate people on Earth. I was given a special talent to play tennis and I did it at a level that I never imagined for much longer than I ever thought possible."
As well as thanking his family, support team and fans, Federer also thanked his opponents from a golden era of the men's game.
"I was lucky enough to play so many epic matches that I will never forget. We battled fairly, with passion and intensity, and I always tried my best to respect the history of the game.
"I feel extremely grateful. We pushed each other, and together we took tennis to new levels."
Federer made his professional debut aged 16 in 1998 and won his first Grand Slam title at Wimbledon in 2003.
He ends his career with a record total of eight men's singles victories at the All England Club.
Federer's last Grand Slam victory was at the 2018 Australian Open
Federer's final Grand Slam triumph came at the 2018 Australian Open when, aged 36, he became the second-oldest man to win a major singles title in the Open era.
After rising to world number one for the first time in 2004, he spent 310 weeks as the leading player on the ATP Tour during his career - a record until he was surpassed by Djokovic in February 2021.
He won Olympics doubles gold for Switzerland alongside Stan Wawrinka at Beijing 2008 and singles silver at London 2012 when Britain's Andy Murray reversed his Wimbledon final defeat from the previous month.
Roger Federer won 20 Grand Slam titles - six at the Australian Open, one at the French Open, eight Wimbledon titles and five at the US Open
He was also part of the Swiss team that won the Davis Cup for the first time in 2014 and lifted a record six ATP Finals titles.
The three-day Laver Cup, staged at the O2 Arena in Greenwich, begins on Friday, 23 September.
Federer is due to join Murray, Nadal, Djokovic, Greece's Stefanos Tsitsipas and Norway's Casper Ruud in a Europe team taking on a team representing the rest of the world.
He was part of the group that created and organised the tournament, first held in 2017.
Federer's retirement announcement comes less than two weeks after 23-time Grand Slam singles champion Serena Williams played what is expected to be the final match of her career at the US Open.
Federer is the most successful grass-court male player of all-time on the back of his unparalleled Wimbledon success, with his last triumph at the All England Club coming in 2017.
The Australian Open was the next most successful major tournament for the Swiss, winning six titles at Melbourne Park between 2004 and 2018.
He claimed all five of his US Open titles in a row between 2004 and 2008, while his one and only triumph on the French Open clay came in 2009.
Federer reached 31 Grand Slam finals in total, doing so for the last time at Wimbledon in 2019 when he lost to Djokovic having held two championship points.
-- Courtesy of BBC Sport