The sword throughout much of our history was merely a status symbol. It did see use and prominent use by some but nowhere near the use in fiction and fantasy.
Spears as CWL mentions were the primary armament. They have gone by many names, specific to different types of spears intended for different tactics but the spear was the premier weapon until firearms.
Even ranged units like bowmen were protected by spearmen.
When it was not spears it was actualy warhammers and maces more often than swords which punched through medieval plate armor that a sword could not. So when penetration of body armor become more important than delivering damage straight to flesh heavy weapons that concentrated a lot of force and delivered thier momentum on small points or spikes become widespread. The sword was horrible for that, while the spear was still effective.
The sword was primarily a status weapon for many, or a gentleman's weapon when dueling was popular during the renaissance. It was more at home on a noble's hip than on the battlefield. There was some places it was quite useful, like with cavalry, because the momentum of the blade, rider, and horse carry the sword through a target in ways well beyond those of someone on foot.
In general though the spear/pike/halberd prevailed.
One advantage of the sword over the spear is that it could be carried informaly. That meant it could be present when necessary for defense or LEO purposes. A spear was large and akward and while better in battle not convenient to carry around just in case.
So in that context the sword was the handgun, while the spear was the rifle in much of history. Guards patroling a city or town could have a sheathed sword while attending to non-combat related activities. A large spear would be less practical. Just as police today are better served with a handgun than a rifle even though the rifle would be better once actualy in a gunfight.
Lastly are the Japanese Samurai. Contrary to movies, foot Samurai fought with spears and bows
As well as other chain or rope based weapons with weights or blades on the end like the manrikigusari, kusarigama or sickle on a chain, and later things like the shoge which were used against them by insurgents or ninjas. They were quite effective at wounding and disabling or entangling weapons or limbs before closing for the kill.
Chain weapons while not always doing a lot of damage directly could entangle a horse, dismount a rider, or deliver a moderate injury while entangling the target and rendering them unable to use superior skill or fight as well while entangled and killed.
Quite simply the sword is a relatively poor weapon against armored opponents, and most professional soldiers used armor for much of history. Though when suppressing the peasants/insurgents where armor was not worn by the enemy they could be more effective.
The sword is also relatively limited in formation, and a formation of trained soldiers defeated a group of men who fought out of formation like they do in most medieval fantasy movies or works of fiction.
Successful conquering armies before firearms used tight formations. Formations where the shield of one man protected another, rendering the formation of soldiers much more capable than the sum of thier parts. They had drills and they could switch formation in combat to suite battle strategies. They drilled and practiced on thier tactics just as modern soldiers practice modern tactics.
The best trained did not break rank under pressure, stress or fear, and thier shield continued to protect the man next to them while moving, attacking and countering.
Of course it is easier to speak of such things than to do them. Stopping a mass of cavalry with pikes is effective and will stop them dead in thier tracks, but being one of the men with large massive horses crashing into them as horses and riders are impaled on the spears is certainly not glorious or safe.
Days after most battles men would continue to die from infections, even small superficial wounds from things like tetanus.