If you’re good at writing with a lot of characters, there’s hardly an upper limit. If you’re bad at writing a lot of characters, your story could feel bloated at 5.
I think the biggest thing is that every character should be distinct and serve a unique purpose. One of the best examples of a well-written story with a lot of characters is… Homestuck. It has its issues, but Homestuck has 16 main characters if you only count the kids and trolls. But they’re all well-written, memorable, and very quickly introduced with their themes & interests. (Furthermore, the trolls being themed after the zodiac definitely helps them be more memorable. If you can connect your characters to a common theme or set of ideas, it’s a fantastic way to make them stick in readers’ minds.) Each of these characters has a distinct personality and serves a distinctly important role in the story.
They also have distinct appearances and names. That’s a big one that I see people overlook. I’m not finishing your story if you start it by introducing me to three generic, skinny, conventionally-attractive pale guys with slightly different shades of brown hair, named Justin, Jason, and Jackson. Maybe you can tell them apart, but I sure can’t. Don’t be afraid to get diverse with your character designs and traits. Please.
One of my personal favorite examples of a good piece of media with a lot of characters is Star Trek: The Next Generation. It has ~7-10 main characters, depending on the specific season or episode. It’s episodic, so each episode focuses on a different character or set of characters from the main cast to be the central focus, but it still incorporates all of them. And they’re all lovable, memorable, and fun characters that work well together. They contrast super well— the forsaken man from a warrior race has almost nothing in common with his friendly android coworker, but they’re still good friends.
Still, I’ve seen/read stories with significantly smaller casts where I quickly got too confused to continue. Having your characters be too similar makes the story muddy, and it makes some characters feel like they serve no purpose. If you can only write two types of people, don’t give your story more than two main characters. But if you’re a skilled writer who can work with a lot of characters, go for it.