Until you've confronted the choice of working with a slung weapon trying to fall off of your shoulder or hula hooping around your torso or of stacking it where it may be out of reach when most needed, you won't understand the handgun's military utility. In theaters of operation which no longer have "fronts" or "rear areas" the handgun makes more sense than ever for service folk who need to frequently not be burdened with a slung weapon.
The military explored "retiring" the handgun in a couple of very serious efforts, one of which resulted in the M1 carbine, the original PDW. Needless to say, the sidearm soldiers on.
Even if the infantry services one day ditch the pistol, the flying arms of those services will not. Additionally, I never see the Navy ditching the handgun as it is just way too useful for searching unfamiliar vessels. Back in the day, I early on humped a Remington 870 aboard vessels to be searched. After a few of those trips, the Weapons Officer issued most of the boarding party 1911A1s instead. It is much easier to climb a ladder or a cargo net with a flap holstered pistol than with a slung shotgun or rifle. It also makes being "ready to go" while opening hatches or watertight doors a great deal easier. Also, there are no closer quarters than below decks on a ship. Ranged and suppressive fire don't really exist as needs in the bowels of a ship and if it is, a shotgun handles the role well.
Besides, in many cultures, the pistol is more intimidating than the long gun or even an SMG. In some places in the world, "assault weapons" are part of the daily backdrop, usually seen on the cops or military police or what have you. However, when a pistol is produced, it signifies in some countries that someone is about to be eliminated.