Lewis Hamilton spent two whole seasons waiting for Mercedes to build him a Formula One car that could lead him back to the top of his sport. But on Thursday, he and Mercedes made it clear that they had run out of patience.

Hamilton, a seven-time world champion, stunned the F1 world by preparing to join Ferrari from 2025, the Italian team said on Thursday evening. The move will end a 12-year run at Mercedes that brought him six titles, 82 race victories, and saw him grow into one of the most successful drivers of all time—he alone holds the records for race wins, pole positions, and podium finishes. It will also bring the longest relationship of Hamilton’s professional career to a close.

The 39-year-old from the suburbs of London had often been rumored to be considering a move to Ferrari, but that noise seemed to dissipate when Hamilton signed a new two-year contract with Mercedes for 2024 and 2025. Instead, the most popular driver in the sport will break that agreement after just one season to join the most popular team in F1 history. There, he will join up with Monegasque driver Charles Leclerc and team principal Frederic Vasseur, who had worked with Hamilton in the lower series before he graduated to F1.

“Mercedes-AMG PETRONAS F1 Team and Lewis Hamilton will part ways at the end of the 2024 season,” the team said. “Lewis has activated a release option in the contract announced last year.”

Hamilton hopes that the switch to the Scuderia will put him in position to challenge for an eighth championship once new technical regulations come into force in 2026 after a spell of crushing dominance by the Red Bull and Max Verstappen, who has won the past three titles.

Hamilton, a go-karting prodigy who had been spotted by the McLaren team as a 12-year-old, joined Mercedes in 2013 shortly after the German manufacturer re-entered the sport. But until that point, his reputation was for being a ludicrously talented racer who wasn’t quite living up to his potential. In six seasons at McLaren, Hamilton had won just one world championship and consistently ran afoul of race marshals for his aggressive driving style.

The switch to Mercedes proved to be the decision of a lifetime. The team was uniquely positioned to take advantage of a series of technical rule changes that kicked in for 2014. And under the tutelage of racing legend Niki Lauda and Austrian team principal Toto Wolff, Hamilton had everything he needed to succeed.

What came next was a period of utter mastery so complete that it made F1 almost boring. Over the next seven seasons, Hamilton won more than half of all Grands Prix to tie Michael Schumacher’s record for world championships at seven. Hamilton’s only serious competition during that stretch was the only man driving the same car, his teammate Nico Rosberg.

All of that came to an abrupt end in 2021. Hamilton lost the title on the last lap of the last race of the year to Verstappen in circumstances so controversial that Hamilton considered quitting the sport altogether.

Though he eventually returned to Mercedes, the 2022 season might have made him wish he’d stayed on his couch. Another set of technical regulations came into effect and it became clear immediately that Red Bull’s new car was vastly superior to the Mercedes. (Even Wolff had to admit to Hamilton over the team’s radio that the car was “a bit of a shitbox.”) For the first time in his life behind the wheel, Hamilton went an entire season without winning a single race.

The 2023 campaign wasn’t much happier and Hamilton wasn’t shy about voicing his disenchantment.

“Ultimately, when you have difficult seasons like this, there are always going to be moments when you’re like, ‘Is it me, or is it the car? Do you still have it? Has it gone?’” Hamilton told the BBC last year. “I’m only human. If anyone in the world tells you they don’t have those things, they’re in denial. We’re all human beings.”

Write to Joshua Robinson at Joshua.Robinson@wsj.com