It really, really depends on the purpose and tactics available. If we're assuming illegal weapons are out, you don't want to engage in symmetrical warfare with military=police, or even drug gangs. Asymmetric warfare becomes necessary, and some parts of that don't even need conventional weapons - one could probably kill their local nazis just with tact and rat poison.
Also, homemade and 3D printed firearms exist (even if their illegality presents challenges if the state has the power to arrest you). I haven't looked into it but I've heard 3D printed firearms are more and more reliable as materials improve.
>Why do soldiers in militaries even have handguns if they already have rifles which are better?
They are effective at killing and threatening people at close range. Rifles are larger and heavier, harder to maneuver if someone is very close, and can't be concealed, that's when a small handgun or knife is useful. Maybe it's also a useful backup if the rifle jams or breaks.
There are probably plenty of military articles/forums that know more about that than me, but it's important to discard the mindset of 'better' and 'worse' in a vacuum. Reality is more complex than that, tools are usually better at some things and worse at others.