To be fair, even Messi, Ronaldo and Neymar don't get paid Federer type of money in endorsements and they are extremely popular and play arguably the most popular sport in the world. They make most of their money on salary alone. Lebron makes more money than them in endorsements so that's not it. It's just the popular and successful tennis players make a lot in endorsements, Federer making a ton.
Tennis players (men and women other then Martina for reasons others have discussed) have always been easily marketable because they play solo sports that aren’t attached to particular cities. There are tons of ways to market them globally that aren’t as easy to do when you are part of a team. I also think them not wearing any helmets/protective gear helps their marketability as their faces are more easily recognizable. Soccer players have this advantage, but they’re tied to teams.
I think basketball players have also been easier to market since there are fewer of them on court and one dominant player can play both offense AND defense
and affect the outcomes of games more than a soccer player or American Football player (QB can but only plays offense) or baseball player (other than a pitcher but he only plays every 5 games),
and because of shoes (see below).
—basketball: 5 on court per team, 10 total, players play both ways
—hockey: 6 on ice, but game is super fast, helmets used
—baseball has 9 per team on the field vs, one batter; batters wear helmets, one person cannot dominate UNLESS HE’S THE PITCHER, but a pitcher plays only every 5 games.
—American football has 11 per team, 22 total + helmets; QB really dominant, but only plays offense and not defense
—soccer has 11 per team, 22 total
Lastly, basketball players also tap into the shoe culture more than any other sport, and this culture is HUGE in social media. You can’t really wear soccer or football cleats or skates every day, but you can wear basketball shoes.