The Swiss great, who has won 20 grand slams, becomes the first of the 'Big Three' on the men's tour to call it a day, with Rafael Nadal and Novak Djokovic still active.
Federer said he was making a "bittersweet decision", having battled knee injuries in the hope of returning to the tour.
Now 41 years old, Federer wrote in a statement on social media on Thursday: "As many of you know, the past three years have presented me with challenges in the form of injuries and surgeries.
"I've worked hard to return to full competitive form. But I also know my body's capacities and limits, and its message to me lately has been clear.
"I have played more than 1,500 matches over 24 years. Tennis has treated me more generously than I ever would have dreamt, and now I must recognise when it is time to end my competitive career.
"The Laver Cup next week in London will be my final ATP event. I will play more tennis in the future, of course, but just not in grand slams or on the tour."
The Swiss great, who won eight men's singles Wimbledon titles, said he would "miss everything the tour has given me".
He returned to Wimbledon this year for a parade of champions, and said at the time he hoped to play there again, but in a competitive sense that will not happen.
Federer said there was "so much to celebrate", adding: "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."
A player whose skills could take the breath away, Federer might trail Nadal by two and Djokovic by one on the all-time list of men's grand slam winners, but to many he will be remembered as the greatest of the trio.
Federer was a virtuoso with racket in hand, going past the previous record of men's grand slam singles wins held by Pete Sampras when landing his 15th major after an epic victory over Andy Roddick in the 2009 Wimbledon final.
He thanked his family and his support team in his announcement on Thursday, and hailed the fans who have always had his back.
Last year, Federer won the ATP's Fans' Favourite award for a 19th successive season.
He said: "You will never know how much strength and belief you have given me. The inspiring feeling of walking into full stadiums and arenas has been one of the huge thrills in my life. Without you, those successes would have felt lonely, rather than filled with joy and energy."
Federer has a financial stake in the Laver Cup, and earmarked it earlier this year as one of two comeback events, along with his home Swiss Indoors event in Basel next month.
However, now the hopes of playing that Basel event look to have been dashed.
His retirement decision comes in the wake of Serena Williams announcing last month she would be "evolving away from tennis".
Although she has tenuously left the door open for a change of heart, Williams' announcement was also effectively one of retirement too, and she was given a rousing send-off at the US Open.
Federer, whose career ran in close tandem with that of Williams, described his own career as "an incredible adventure".
"While it sometimes feels like it went by in 24 hours, it has also been so deep and so magical that it seems as if I've already lived a full lifetime," he added.
"I have had the immense fortune to play in front of you in over 40 different countries. I have laughed and cried, felt joy and pain, and most of all I have felt incredibly alive."
He recalled being "a ball kid" in Basel in his youth, and seeing players of a past generation at close quarters.
"They were like giants to me and I began to dream," Federer wrote. "My dreams led me to work harder and I started to believe in myself."
Federer had a reputation early in his career as a racket-smasher, and it was not clear that he had the temperament to maximise his talent.
That soon emerged, though, and Federer explained: "Some success brought me confidence and I was on my way to the most amazing journey that led to this day.
"So, I want to thank you all from the bottom of my heart, to everyone around the world who has helped make the dream of a young Swiss ball kid come true."
He added: "Finally, to the game of tennis, I love you and will never leave you."