In those games an "optimized" group tends to have certain roles covered. This was a common practice for so long that a lot of experienced players will default to trying to "fill roles" around this when it comes to making characters - and in some cases will complain about their being gaps if people make their characters without knowing what everyone else is making.
Newer games, even mainstream games like D&D 5e, have tried to break this but it is very heavily baked into the lore as much as the mechanics. Even fantasy, sci-fi, anime, and comics will play into these roles when there is a team more than a solo or duo book. So what are they?
Disclaimer: Using D&D Terminology
As a disclaimer I'm going to be using D&D terminology for the most part here. Why? Because in a lot of ways D&D is synonymous with RPGs when it comes to casual conversation, and because D&D terminology has spread out into videogames and other games that fill similar roles. I'll try to make notes for non-fantasy settings when possible.
The Four Man Band
The classic four man band is: the tank (fighter), the rogue (stealth), the healer (cleric), and the arcane (mage.) This at its base covers the general problems you expect an adventuring group to come up against. The tank/fighter handles in close melee combat, controlling targeting and the fight in general as best they can. The rogue handled scouting, anything that needs a mundane skill check, and doing non-magical burst damage to any priority targets (often whomever the fighter is primarily trying to control.) The healer keeps the group standing. The mage provides ranged combat support, generally in the line of AOE damage or status effects/debuff. Both the Healer and the Mage overlap a bit in utility.
The Five Man Band
When the group goes up to 5 man, the most common addition is another fighter. This comes in one of two flavors: a ranged fighter (often a Ranger in classic D&D but not necessarily, especially in 5e) that adds single target, ranged sustained damage to the groups repertoire. Alternatively you get a damage dealing in close combatant. This then differentiates the first fighter into the "tank" while the second fighter type is the "damage." One focuses more on control, buffing, and staying up/in the way. The other focuses on sustained damage. The sustained damage type is, traditionally, a barbarian but can also just be a Fighter. Especially if the other fighter is a Paladin.
The Six Man Band
The six man band is where you have a lot of freedom. After all, the four man band covered the 'big' tasks. The five man band added in a quality of life improvement for combat and other engagements based on the most likely need (spells run out, fighters tend to stay useful even if out of arrows). The sixth man thus can fill other needs. This is generally where you'll see a more pure utility caster come in like a Bard, taking over the buff/debuff game from the Healer and Mage and freeing them up to focus on their primary job.
Why Are These The Standards?
I know there are counter examples to these, but in general these seem to be the default? But why are they like that? Well, I mentioned it in the explanations but it is because these jobs - coming in at these times - handle the problems that you are most likely to face in the game. And most RPGs are focused on combat (more and more indie games are thankfully breaking from this, but mainstream RPGs still heavily focus on combat for mechanics and balance.)
What problems are you going to run into in a game?
- Big damage dealers like rogues/mages are squishy and need protection
- Answer: the fighter
- Dungeons are full of traps, a need for scouting, and skill checks
- Answer: the rogue (often equipped with special abilities or extra skills to handle those things)
- People get hurt, and you need to be able to keep going while in dangerous territory
- Answer: the healer (more on this later)
- Sometimes you face large groups of enemies that are impossible for the fighter to control
- Answer: the mage (or nuker who does the AOE to kill groups fast>)
With those out of the way you have limited problems you are running into. An extra fighter for more control, or more ranged options to deal with enemy ranged combatants that the mage can't get to is always good. That's why the 5th man often falls into one of those categories. After that you need something more akin to a generalist to "catch all" the problems not already heavily covered. Which is where classes like Bards and Druids come into play with their unique skillsets.
Healing vs. Buff/Debuff/Control
For a long time it felt like even if you had Buff/Debuff?Control you still needed a healer. Thankfully games are moving past this now in different ways. 5e for example provides a lot of healing to various classes all over the place in the form of Short Rests, healing abilities being more widely spread across classes, and how powerful a long rest can be. On top of this, the ability for buff spells, debuff spells, or crowd control spells to flat out prevent or mitigate damage has been embraced enough as a playstyle that it can stand in for healing. After all, it doesn't matter if you healed 25 points of damage, or prevented 25 points of damage - either way, 25 points of damage are not a problem anymore.
Variation and Variants
Now we get to the fun part. How did those roles become the standard? Like I said, they answered the question of "how do we solve this common problem?" This has shoe horned some groups and players into specific roles/classes/jobs because people only accept the 'standard' answer. But the fact is, you can break from tradition. You - and your group - just have to do one of two things: find an alternative way to answer the problems, or accept that those problems will be larger problems than perhaps otherwise accounted for.
For example, Monks in D&D 5e can cover for a lot of what a Rogue does in regards to stealth and scouting, while providing an interesting mix of crowd control, combat capability, and buff/debuff. A monk could cover some of the problems a Rogue solves, and make it so that a group doesn't miss the lack of a rogue provided the job of "handles skills" is also spread out across the group.
So how does this work in non-fantasy settings? Well, more or less the same way. The jobs just change their description a bit. If you think about it in job function more than fantasy coded names it becomes easy.
Fighter becomes Tank - or brick wall, shield, etc - because their job is to 'tank' the damage for the group.
Rogue can stay rogue, but often is covered by 'stealth' for what its core job is (sneaking around and taking people out.)
Mage becomes 'nuker' or just AOE.
Healer stays healer in most games, but is sometimes called Medic, Doctor, or just Crowd Control.
Jobs to be covered by the 5th or 6th man also comes down to the setting. Cyberpunk games bring up the problem of how do you handle the Net, or how do you handle Vehicles, which brings up 'hacker' and 'driver' character types - or jobs to be covered. Games with a strong social aspect to them (Shadowrun for getting jobs, court intrigue games, etc) have a need for 'Face' or 'Talking' characters - something else the Bard in D&D often covers, or contrasts with Paladins and such for.
Problems -> Solutions
Ultimately understanding how a 4, 5, or 6 "man" band is going to work for your game of choice - or with your GM of choice - comes down to understanding what problems are going to be faced by the group. Some games have standard, implied problems. D&D for all its strengths is guilty of this, and while working against it a group of "Fighter/Rogue/Mage/Cleric" is going to be more 'optimal' and what written adventures are built around then some other mix. Not to say you can't get by without it, but it is what you'll see modules were written expecting.
Viewing This As A GM
You can also use this as a GM. Look at your group. What problems are they built to solve? Those problems should show up in some form so that the PCs can overcome the problems they built themselves to overcome and feel strong. What problems are they less well equipped to handle? Those problems should come up to challenge them at times.
And if you break the group up, which solutions are going where? How can you challenge those groups?