Why would ancient troop formations have more than a few ranks? It seems to me that you would want all of your soldiers to be fighting the enemy at once; why would you then put a large number of your soldiers out of range by putting them in the 4th rank or more? Did they rotate men out of the front ranks, or were they just there to fill in the gaps when the front-rankers died?
This kind of question has come up quite a bit lately. The two threads linked below address the issue, especially the first where the (likely fallacious) idea that the rear ranks in formations pushed like in a rugby scrum is addressed.
Executive summary: Ancient battles weren't about everybody whaling on everybody with swords and spears. They were about breaking the enemy's morale, breaking their will to fight, inducing them to panic and flee.
It is uncertain how many of the participating soldiers would actually engage in a close-up hand-to-hand fight, but the figure was likely quite low.
The extra ranks in a formation served to intimidate the enemy, to make sure your own guys wouldn't run away, to make sure that your line couldn't be easily penetrated and then surrounded and to enable you to actually manoeuvre^1.
Also, a very wide and thin formation would stretch over many kilometres, which would make it very difficult for an ancient commander to actually command his army or even see what is going on. Philip Sabin notes that very large ancient armies tended to deploy in much deeper ranks than their enemies, but not much wider. He thinks it was for this reason. See for example Pompey v.s. Caesar at Pharsalus.
^1) A deep and narrow formation can move very easily and still stick together. A shallow and wide one, on the other hand, will break up into bits and pieces if it tries to move. This is shown quite clearly in the age of musket warfare, where infantry did deploy in very wide and shallow lines to maximise firepower. Attacking with bayonets, though, was much easier when deployed in columns than when deployed in lines.
Thread 1: Did the people in the front lines of ancient armies basically know they are going to die?
Thread 2: How could ancient and medieval battles last for hours?
I can't speak for other civilizations, but a Macedonian Phalanx would be made up of pikeman, each with a sarissa up to 20 feet long. The problem with a pike is that all the killing power is concentrated in a few inches on the end, and if an enemy manages to get past that few inches then you've got no way to attack him (short of dropping the pike and drawing another weapon) and he has a clear path straight to you. However, if you have a man behind you also armed with a pike the enemy has to get past your pike point and then his to get to you. If the ranks are deep enough, then the enemy has to face the points of the pikes along his entire route to you. So in a pike phalanx, the more ranks the better.