Fighting in lines or shield walls works because the whole is greater than the parts.
And this is something pretty much every movie ever gets completely wrong. I can't really think of any movie right now in which a big battle without firearms is shown with any degree of realism. The only scene I can think of that comes close is in The Two Towers when the orcs are breaking through the big door of the castle. What the defenders inside the door are doing comes pretty close to how battles were actually fought. Using the remains of the door as big shields for the guys in the front, and the rows in the back bracing them from behind, while they try to poke at the orcs outside with spears through holes in the wood. And the orcs are trying to push the defenders back through sheer force, while also trying to stab at their faces with spears.
Here you see the attackers making a suicidal and completely uncoordinated charge, while the defenders form a totally crappy attempt at a shield wall. There is nobody behind the frontline guys to brace them against the impact, and it happens exactly what you would expect to happen. They fall over.
But almost any other time, you get all these armies lined up as a single force, and as soon as they get close to each other they completely break formation with everyone individually throwing himself into a sea of whirling blades. Do you sometimes watch such battle scenes and wonder "how can they keep fighting without getting stabbed from behind, and who has the discipline to run into that blender?" Well, the answer is they can't and nobody does. It looks completely suicidal because it is.
Here we have a pretty good demonstration how it is supposed to look like, from a movie that doesn't even have the slightest pretense of being anything resembling realistic. But in an actual phalanx battle, this is how they would stay throughout the whole fighting.
There is a nice moment in The Hobbit 3 where the dwarves put up a really cool looking shield wall that is extremely well armored. And then those silly elves just jump over them into the orc horde and the dwarves say screw it and also abandon the whole maneuver. It is like the director saying "Hey, I know there are many of you who would like to see a somewhat realistic fight scene! Well, screw you, I won't give you any!"

And you
never ever run horses straight into this. Because you and your horse will 100% be dead. How it works is that the cavalry gets as close as they can without getting into reach of the spears, then throw their own spears, and all riders simultaneously turning their horses around and get away. Then they get new spears and do it again, over and over, each time maybe taking out a few defenders with a lucky throw that didn't get blocked by a shield. A bit more advanced is to do the same thing with lances that are longer than the spears of the defender. Then you don't have to throw your spear at the enemy (who could very well throw it back at you), but can use your speed and weight to have your lance make a very heavy impact on the shields. But you still all have to make the last minute turn before being impaled. Even in the late middle ages, when knights were very well armored, the horse was still vulnerable to some degree. The impact of a spear from a charging horse is pretty heavy, so you have to have a second and third row behind the first line of defenders, just to keep the ones in the front from being pushed over.
However, the defenders will still have some few small casualties and when you keep it up long enough (and actually can keep it up that long), eventually some individual defenders might think that the next charge might get them stabbed with a spear and try to run away. And then they are usually all very dead. A running man on foot has very little chance against a rider coming from behind and that's when you get total slaughters. If you know you are not going to win, its best to move backwards slowly and in order to leave the battlefield. With some luck your enemies will not pursue you. But if you turn and run, you're probably dead.
The Battle of Hastings took all with the Normans charging the defenders shield wall about a dozen times with no real effect. The Normans just couldn't get their enemies scared enough to run away in chaos, so instead they pretended to flee, so that their enemies charged forward in chaos. Then the riders simply turned around and all the defenders who were spread out over half the battlefield collectively thought "Oh Shit!" And that was the end of the Anglo-Saxon kingdoms.