While the answer will be vastly different depending on culture and time period, the short answer is that basic pre-gunpowder battlefield tactics required infantry to be close, compact, and orderly.
Generally, swirling and pirouetting one-on-one sword-fights in any pre-gunpowder conflict is an invention of modern cinema. Sure, if you really want to be pedantic, Bronze age cultures did conduct warfare that way (probably) but for the most part, a fighting force is only as strong as it is united and disciplined.
So by and large, pre-modern battles were fought between tight formations with polearms. Again, there are exceptions: imperial-era Roman Legions emphasized order and discipline but allowed for a fair amount of space between ranks, and cultures who fought predominantly on horseback had a whole other system as well. But I'm most familiar with high medieval Italian militias, who fought battles principally characterized by infantry slowly grinding each other out. In cultures where organized mass mobilization existed, this was almost always the predominant system. In Europe, opposing cavalry typically engaged each other on one or both flanks in order to try to wrest superiority, and subsequently try to to encircle the opposition. A horse won't charge head-on into a static wall of spears, but unsupported infantry will break and run if flanked by charging cavalry. There are differing opinions on how the specifics of engagements played out: some argue battles were short and bloody affairs, while others argue two battle-lines would reengage and disengage for multiple short periods over the course of the day's engagement (I'd say it was probably a little bit of both depending on the situation). But what is certain is that in the melee, unity is strength.
I'd also add it's important to point out that large-scale battles were the exception rather than the rule in European medieval warfare. Large, decisive battles between evenly-matched forces were risky business, and most leaders tried to avoid them. During the skirmishes that could occur between raiding or scouting parties, more open and less structured clashes could and did definetely occur. But records of these smaller clashes very seldom exist, because insofar as the leadership was concerned medieval warfare was mostly a slow, drawn-out affair that mainly consisted of ravaging, destroying supplies, and laying siege to strategic points. Some of Europe's most famous warmongers fought surprisingly few battles: in the tenth century, the German Kaiser Otto I usurped the crown of Italy and there is not a single record of a pitched battle; in the eleventh century, William the Conqueror fought a single pitched battle in his military career, and that battle made him King of England; the following century, Emperor Barbarossa managed to reassert imperial control in Italy by focusing on sieges and controlling strategic points, but lost everything when the Italian forced him onto a decisive battle.