Is there a proper way to handle large combat engagements with party members involved?

For instance, a party of four player characters raise a force of 30 militia men to go take a bandit camp. When they get there the bandit camp has 40 men, and none of them notable.

For the sake of simplicity, both the 30 men and the 40 bandits are armed with long swords, and wearing leather armor.

Is there a proper way to handle combat between the 30 man militia and the 40 bandits while also incorporating our four player characters? If no "proper" way exists, is there a generally accepted "best" way?

The RAW rules simply don't have a way to handle a 70-creature battle without it being excessively cumbersome. However, there are three things that I think will help you on this.

Unearthed Arcana: Mass Combat

This Unearthed Arcana provides rules for tracking mass combat. You organize groups of creatures together and treat them as single units, with some very simplified rules for what happen when two groups fight. The cool part about this rule set is that the player characters are not a part of the unit groups - they can act outside of the group restrictions for the rest of the NPCs and swing the tide of battle by eliminating units from an enemy group, casting spells, or otherwise contributing to the battle.

If your characters have interest in a more war-game style of play and want to be involved in the whole battle, I'd go this route and let them each take command of a group of militia. These rules are super fun, but don't make the fight much more complicated than a normal fight of a party versus several enemies. The only downside is that the party has to learn a new set of (very simple) rules - this will be easy for some parties and a challenge for others.

Narrative Focus

Rather than trying to keep track of the entire battle as a whole, treat the majority of the battle as a backdrop. Have in mind a rough idea of how things are going to go each round without player intervention (for example, maybe 1d6-1 guards and 1d4-1 bandits die every round), and each round narrate how the battle is going around them. However, focus specifically on the slice of the fight immediately around the party. For example:

As you and the militia charge in, fights quickly break out around the perimeter of the camp. You hear the shouts of battle and the ring of sword against sword, and see both bandits and militia taking wounds, retreating, and falling in battle.

You charged in with the right flank of the militia attack. Ahead of you, you see five bandits here, here, here, here, and here. Additionally there are two bandits here and here armed with crossbows. With your are three militia here and here, as well as the militia captain here. Roll initiative.

Until they finish their fight, you don't really need to keep track of anyone outside the scope of that battle at all. You could periodically give a brief descriptive update ("looks like the battle on the south flank is going poorly for the militia, but a group of militia to the east have successfully eliminated the sentries and are fighting their way into the camp."), but I'd stay away from anything more than that. As enemies and allies die, introduce new NPCs on either side as seems appropriate ("three more bandits stream in with swords drawn").

For a great example of this, see the Battle of the Hornburg in the second Lord of the Rings movie. There's definitely a sense of a huge battle, but we don't spend an inordinate amount of time tracking the battle as a whole. Instead, the focus of the narrative (the camera) zooms in on the section where our heroes are acting. We'll see Aragorn, Gimli, and Legolas repel orcs trying to climb their section of the wall and save their elf allies (in a small and manageable fight), and then the narrative zooms back out for an update on the status of the battle as a whole. After their fight on the wall, they realize that the battle is going poorly at the gate and rush over to help defend the gate, at which point they go into another smaller and more easily-managed fight. Aragorn and Gimli only ever face maybe 4-10 orcs at a time, with more coming in to replace those that fall until the fight is over.

DMG: "Adjucating Area of Affect" and "Handling Mobs" rules

On page 249-250 of the DMG, we see some additional rules for mass numbers of mobs that I'd recommend you read. Whatever tactic you take, this should help speed up the pace of battle. "Adjucating Area of Affect" provides some easy guidance for determining how many enemies are hit by an Area of Affect Spell. "Handling Mobs" provides a way to calculate the success of a large number of creatures making an attack without having to roll dice for each. If nine bandits shoot at your Barbarian and they need to roll at least a 13 to hit, then you can just say that three of them hit without having to roll dice.

The rules for Handling Mobs may help

As an alternative for the Mass Combat rules from the UA, consider using the Mob rules from the DMG. The DMG includes a section for Handling Mobs that is designed for a few dozen monsters:

Keeping combat moving along at a brisk pace can be difficult when there are dozens of monsters involved in a battle. When handling a crowded battlefield, you can speed up play by forgoing attack rolls in favor of approximating the average number of hits a large group of monsters can inflict on a target.

Not quoting the whole thing here but the main idea is that you group monsters up and instead of rolling individually you just determine how many of them hit depending on how many monsters are attacking.

Preparation before the Combat

When using this method, it is very useful to calculate some numbers beforehand so you do not have to keep calculating everything during actual combat. What I did was note the HP, attack bonus, damage and AC of all the parts involved (including AC of participating characters that are not in mobs, such as PCs) and create a table that told me how many monsters were needed to inflict how much damage on each target. This is not perfect as AC variations are possible during a combat but it does help a lot in expediting things.

Here is an example on how part of the table might look for your situation (using bandit and guard stats as an example):

$$\begin{array} {|r|r|r|} \hline \text{Attacker} & \text{Defender} & \text{Damage per number of attackers} \\ \hline Bandits (Scimitar) & Guards & 4/3 \\ \hline Bandits (Javelin) & Guards & 5/3 \\ \hline Guards & Bandits & 4(5)/2 \\ \hline \end{array}$$

• The bandits have a +3 to hit against a guard AC of 16 which means a roll of 13 is required to hit. This gives us 1 hit per 3 attacker according to the Mob Attack rules, which using average damage means 4 damage per 3 bandits when using Scimitars and 5 damage per 3 bandits when using Light Crossbows.
• guards have a +3 to hit against an bandit AC of 12 which means a roll of 9 is required to hit. This gives us 1 hit per 2 attackers, doing 4 or 5 (with two-handed) damage

My own experience and homebrew rules used

This suggestion comes from having used Mob rules in an encounter when DMing the Curse of Strahd campaign (spoiler alert naturally):

In the Village of Krezk, Abbey, a fight was triggered when the players released about 60 Mongrelfolk, involving the PCs as well as a few other NPCS.
Admittedly, 60 monsters is considerably less than the number you are using (around 200 total from my calculations) iirc I used about 8 groups of 5-15 mongrelfolk with a few variations of abilities, some were actually helping the players, some against, and others just doing their own objectives

I found the Handling Mobs rules, being aimed at handling multiple creatures attacking a single creature to be insufficient for squad vs squad fights so I will detail what else I used, emphasizing the fact that these are my own homebrew rules that I used and not official in any way:

• Creatures were grouped into squads that acted on their initiative order. The total initial HP of the squad is the HP of a single member multiplied by the number of creatures. The number of remaining members of the squad for attacking purposes is the HP of the squad divided by the HP of a single creature (rounded up). I actually just kept track of the number of creatures left and the HP needed to kill the next creature.
• Example: a squad of 15 bandits would start with 225 HP. If the squad takes 57 damage, reducing it to 168 HP, I would note that 57 = 12 + (3*15) meaning 3 bandits die and another is reduced to 3 HP, so I would note this squad now has 12 bandits, 1 of which has 3 HP left (if you ever played something like Heroes of Might and Magic, this is pretty much what I mean).
• Similar to how swarms worked in previous editions, I allowed AoE damage spells and effects to deal 1.5*damage to squads.
• When a medium or small creature gets attacked by a squad in melee (PCs getting heroic, for example), for a medium sized creature squad, only at most 8 creatures attack the creature (adjusting for different sizes as appropriate).

Final notes

When I used this set of rules, the fight had about 60 creatures in about 8 squads plus about 5 individual creatures participating. That was 13 initiative turns per round and the fight took quite a long time to resolve, though mob turns were the faster to resolve since no rolls were needed. Still, the fight was about two and a half hours and over 10 rounds of combat.

My players did enjoy it and it is still quite a memorable session for them, especially given that their characters were able to influence the outcome of the battle with their own PC abilities but my players enjoy tactical combat quite a lot so YMMV there.

Running a combat encounter like this involves a lot of mental calculations on the DM. If you have a lot more creatures involved this may be even worse for you. Having players keep track of friendly squad status themselves may help, or simply reduce the numbers of participants to a more manageable amount (ex: handling a more localized fight with only a few squads).

My biggest piece of advice is to have a group initiative. What that means is, instead of rolling 30 initiatives for 30 separate grunts, just roll 1 initiative die for all the grunts, and they all attack together. For example, in your case, give the militia a single group initiative roll, and the bandits a single group initiative roll, and then attack with them all together. In terms of stats for group initiatives, you could use the average of the bandits' initiative scores (dex), or if they all have about the same initiative score, just use that. For example, if the militia's initiative roll is nat 20, they all go first in the combat - vice versa, if the bandits' initiative roll is a nat 1, they go last in the combat.

In terms of hit points, that would partly depend on the levels of the people in the groups. IF they're just level 0 nobodies, for example, I'd give the militia 60 hp and the bandits 80 hp - two for each guy. Then, for every two damage inflicted, one of the grunts keels over. This way, when one group reaches low hp - such as, the bandits only have 15 hp left - the remaining grunts could surrender or flee.

Finally, for attacks, you could roll 30 attacks against 30 ac's, but that would, of course, take a long time. Instead, again for the militia of 30 people, you roll 6 attacks - each attack counting as five people. If it hits, roll the total damage of those five people. If it misses, those five people weren't that productive that combat turn, and couldn't land attacks. It's a bit wasteful, but it keeps the combat moving at a relatively quick pace.

This is all just my two cents and how I would tackle the situation, so take it with a grain of salt, but I do strongly reccomend at the very least using group initiative so you aren't keeping track of twenty separate, easily mixed-up NPCs.

Another option is to sort of "pre-run" the battle, according to mathematical odds, and a few rolls thrown in.

I have a party in my campaign that has taken sides in an existing conflict, and in a few months will probably be part of a giant battle, fought underwater, between a Kraken, an Aboleth, and tribes of Chuul, Merrow, and Kuo-Toa (on one side) vs. about 2000 Goblinoids with Water Breathing cast on them (on the other side). The party has allied itself with the goblinoids, and will be casting the Water Breathing spells for the whole army, in order to make the contest even possible to happen.

I'm not even going to try to handle rolling that whole combat at the time -- I'm going to work out the main course of the battle ahead of time, who does what to whom and when, as those sides exist, and narrate that as we go, modifying the course of the battle based on how much the actions of the characters change the flow.

I plan to deal with initiatives and attacks and all of that on the spot only for the PCs. and whoever they end up fighting directly.