The 36-year-old beat Robin Haase 4-6 6-1 6-1 in the last eight of the Rotterdam Open to replace Rafael Nadal, 31, at the top of the standings.
Federer surpassed eight-time Grand Slam winner Andre Agassi, who was 33 when he was deposed in September 2003.
"This one means the most at 36 years of age, almost 37," the Swiss said.
"When you're older you have to work double the amount. You have to wrestle it back from someone who's worked hard to get there.
"Reaching number one is the ultimate achievement in tennis. It's been an amazing journey and to clinch it here, where I got my first wildcard in 1998, means so much."
Federer's hopes of reaching the semi-finals in the Netherlands looked slim when he lost the first set, but he dropped just two further games to set up a tie with Italian Andreas Seppi or Russia's Daniil Medvedev.
At the end of the match, the 20-time Grand Slam champion sat down and wiped away tears, before returning to the court to take the acclaim from the crowd and receive a trophy for his achievement.
He first became world number one in February 2004, but has not topped the rankings since October 2012 and slipped to a low of 17th in January 2017.
That was after he spent six months out recovering from an operation on a knee problem.
However, he has since won eight titles, including Wimbledon last year and two Australian Opens.