The infantry square is a tactic used against cavalry. In this YouTube Video, it is shown in Rome Total War how 8 units form a square, with an additional 2 units reserve, defeated a cavalry force twice as big:
One should observe in the video (e.g., at 0:54 et seq.) how the cavalry charges into the corners.
The corners are the weakest part of the infantry square formation. You can see at 2:14 how the cavalry attacks the corners repeatedly.
First we should note: an infantry square has neither flanks nor a rear. This is important, because the Macedonian "Hammer-and-Anvil" tactic fails in this situation.
Heavy Cavalry historically worked by flanking the infantry line, eventually causing the entire army to rout and collapse.
But observe: this works if and only if there exists a flank.
As observed in, e.g., the YouTube Video using Rome Total War, the infantry square formation has only fronts. This means any cavalry gets the pike line.
Note the square's weakest points are the corners.
History of the Formation
Although it is mentioned in Plutarch's Lives, specifically the description of the Battle of Carrhae:
NB: the Romans did not enter the square formation as defence against cavalry.
All were greatly disturbed, of course, but Crassus was altogether frightened out of his senses, and began to draw up his forces in haste and with no great consistency. At first, as Cassius recommended, he extended the line of his men-at‑arms as far as possible along the plain, with little depth, to prevent the enemy from surrounding them, and divided all his cavalry between the two wings. Then he changed his mind and concentrated his men, forming them in a hollow square of four fronts, with twelve cohorts on each side.(Plutarch, Life of Crassus, 23.3)
I am told the Chinese adopted a square tactic when dealing with Xiongnu nomad armies in the 1st century AD...although I cannot find a source for this.
As far as I can tell, it wasn't until the 14th century that the tactic was used specifically against cavalry.
This tactic was derived from the "schiltrom", which is derived from the Middle English word scild-hreóða meaning "the arrangement of shields as in the scild-burh" (Bosworth and Tuller's An Anglo-Saxon Dictionary, pg 831).
Although the first recorded instance of this tactic in English history is William Wallace's troops at the battle of Falkirk in 1298 (see, e.g., Fisher's William Wallace, pg 80), it may be believed the tactic existed prior to the battle. Note this is the circular formation.
The rectilinear formation was, in English history, formed around 1300. However, unlike the circular schiltrom (which was used for defence), the rectilinear formation was employed for offence and defence.
Robert the Bruce at Bannockburn (1314) first employed rectilinear schiltrom offensively.
We will examine the predecessor to the "modern" infantry square (the so-called "Spanish Square", or Tercio), and then we will examine the "modern" Napoleonic infantry square.
The Spanish Square dates back to the (Great) Italian wars (see, e.g., Davies' The Golden Century of Spain 1501-1621 for a brief description of the birth of the Spanish tercio).
Personally, I find this a bit misleading. The Spanish Square looks more like the "5" side of a die, with the center consisting of pikemen and the other units arquebusiers. There might have been an additional unit of musketeers. This is a bit different than the infantry square tactic.
The Napoleonic version dates back to 1745, British General Richard Kane's "A New System of Military Discipline for a Battalion of Foot on Action; With the Most Essential Exercise of the Cavalry, Adorned with a Map of the Seat of War and A Plan to the Exercise" (eprint) details the usefulness for infantry squares against cavalry.
David Chandler's Military Campaigns of Napoleon notes that at the battle of the Pyramids, Napoleon formed squares "to increase the firepower of the exposed sides" (224).
At the Battle of Mount Tabor (16 April 1799), Kleber formed "small French squares" (Chandler, 239). It carried the day.
Colonel Winkel's Saxon genadier battalion formed a square to hold off French Cavalry (commanded by Murat) on 14 October 1806 (Chandler, 486).
Gudin's division had its Demi-Brigades form squares (Chandler, 490) when facing the Prussians at the Battle of Jena-Auerstadt on 14 October 1806.
The last example: during the American Civil War, the Thirty-Second Indiana Volunteer Infantry at the Battle of Rowlett's Station, December 17, 1861 and used the square formation against Terry's Texas Rangers. This is the only recorded instance of the infantry square during the American civil war. (The outcome of the battle was inconclusive, both sides claimed victory.)
When repeating weapons came into use, both cavalry and concentrated formations began falling out of use.
Consequently squares are no longer used, except in training (as I understand it).
I was talking about this formation with my dad, and he remarked since the Napoleonic wars it become standard for cavalry units to have horse artillery. Why?
Well, if a cannon hits the infantry square, the cannon ball comes in one edge and out the other.
That's the major weakness of infantry squares: vulnerable to gun fire (viz., artillery fire).
This vulnerability is probably why infantry squares were absent during the Civil War...