The spearman is a very common ancient unit kind that appeared as early as in the Ancient Age. These soldiers were outfitted with spears: polearms that have a sharp head made of stone or iron. The length of the polearm allowed for striking the enemy from several feet and holding the weapon in two hands, making it much more reliable than most one-handed weapons in the time.
Spearmen started appearing in military ranks in big numbers, most famously in ancient Greece. Hoplites, spear-wielding shield infantry were the backbone of Alexander I the Great's army: shields raised above the head and spears hold in the front formed an almost impenetrable group that could march slowly and offered great protection against arrows and small rocks. The phalanx was notorious for its reliance on army discipline: a low-morale army would quickly scatter and make no use of this formation.
With the appearance of castles and cavalry in the Medieval Age, spearmen became more important than ever in regular armies. Spearmen often got very long pikes or poles with little shielding to tackle charging horsemen, often stabbing or pulling the rider from the horse so that their speed advantage quickly diminished. The spear proved to be more effective than the sword or the bow in this case: a well timed and pushed spear into uncovered surfaces of the knight or the horse could incapacitate them or make them unable to fight.
In castle garrisons, spearmen had a notorious advantage on the castle walls. When the enemy prepared and pushed ladders against the castle wall, spearmen could push the ladders back with the help of their pikes so that the ladder fall to the ground, carrying its climbers into death. The spear was also a good close combat weapon when fighting close up. It was very slow in striking however and its unwieldiness in confined spaces rendered it unusable at times.
Many spearmen in open field battles were utilized to not use spears as melee weapons, but rather a thrown weapon (called javelins). Javelineers carried several spears with themselves and threw them in the direction of the enemy when constantly approaching them, then switched to a more efficient weapon close-up, such as daggers or short swords. This tactic was most effective against light infantry, as the thrown pike could cause fatal injuries, but this could be used as a distraction as well against ranged units.