With spears (and later pikes), it's all about the rank bonus. For spears, each supporting rank (up to 2) gives +1 defense and +1 charge, and a second supporting rank gives +1 to attack. So, in theory, a spear unit with at least 3 ranks of men gets +2 defense + 1 attack, and +2 charge. In practice, the rank bonus can be disrupted by trees, obstacles, and poor formation control. You can tell how well a spear unit is holding formation in combat by the individual attack animations, the front man and his backup in the supporting ranks will all be performing the animation. Pikes can support up to 4 supporting ranks, for a max +4 defense, +4 charge, +2 attack.

When charging, the formation may get disrupted and lose the rank bonus, and the charge attack bonus for spears generally won't last long anyway, so it's better to stay in formation and grind it out with the rank bonus. Spears should be the anvil of your army, holding enemy units in place while your cav and other flankers get in position to charge from the side/rear.

On the flip side, when attacking spear units you want to negate the rank bonus. Attack the side or rear, force them into trees, break up the formation.