Depth is important to be able to withstand charges, width to avoid flanking. Usually the pre-defined depth at deployment is enough for withstanding charges, except for lighter units where increased depth (compared to the default depth) can be useful. Depth also sometimes help morale, from what I've seen. When the unit gets mixed up with another unit it routs more easily, and the risk of that increases with smaller unit depth. But if you're not facing any heavy cavalry or shock infantry you can make the formations quite thin if you really need an extended line to avoid being flanked, however it's a matter of balance to avoid making it too thin so that the enemy can rout it before your cavalry hits them to the rear - hammer and anvil usually doesn't work as well when the anvil is gone.

But because there's almost always some cavalry involved in the fight and it can strike almost anywhere along your line, it's usually best to simply stick to the default unit depth in my experience. In multiplayer I'm usually able to rout players with too deep units fairly easily by flanking them. However a square shaped unit formation can be useful if you need to react really quickly to hits from any direction, which I've found out the hard way in mp. I haven't tested myself, but I think it's likely that square shaped unit formation would be very useful for spearman reserves behind your main battle line.