Handguns saw extensive use during the First World War, largely because they were far easier to wield in the cramped, close-quarters environment of trench warfare. A long rifle simply wasn't practical in such tight spaces.
Since then, handguns have primarily been issued to troops who would struggle to carry a rifle due to confined spaces—think armored crewmen, transport drivers, and clerical personnel. For similar reasons, submachine guns are also commonly issued in many armies, offering compact firepower where a full-length rifle would be unwieldy.