I don't know if this is the right place to post this question but I don't think there's a sub called /r/AskMilitary.
Anyway, why was this so good against cavalry? I'd say that the best way to counter a cavalry charge would be to have as many rifles firing at once, but that's the opposite of what the square formation does, it even has men facing the other way, but of course I'm not a tactician so I'd appreciate the help
One thing you have to keep in mind is that during the period that the infantry square was used, rifles were not particularly common. The battlefield weapon of the era was the musket, which is far less accurate.
The purpose of the square formation is quite simply to negate the advantages of cavalry. A man on a horse is faster, heavier and scarier than a man on foot. The smallest unit to form a square would have been a battalion, consisting of some 500-1000 men. For quite obvious reasons, a thousand soldiers moving in formation won't have the easiest time trying to manoeuvre and a unit of cavalry would easily be able to flank them, taking advantage of the ensuing chaos as the infantrymen tried to wheel around to face the incoming charge. A square formation, by virtue of having men with pointy shit (i.e. bayonets) attached to their muskets, is quite difficult to flank.
The next part is that while it might seem clever to stand in a line and point all available firearms at the incoming cavalry, it isn't quite as simple. The approaching cavalry won't be charging straight at the middle of your line in an orderly fashion; if they target the right flank of your line, you'll end up with the other half trying to move up so they're perpendicular with the right flank. Except they won't, because then they're exposed to the second unit of cavalry that comes swooping down from the treeline over there.
Then there's the fact that a mass of guys on horses carries quite some momentum. If you're standing in a line, you might be able to shoot one horse before it reaches your line and, if you have balls of steel, bury your bayonet in another, but what then? A charging horse won't come to a full stop just because it got a foot of steel in its chest. Even if it dies, the momentum will carry it forward into the line and suddenly you've got some hundred lbs of dead horse on top of you, providing a nice gap for the other cavalrymen to exploit.
Bear in mind that this would happen everywhere in the line, effectively breaking it up. This is provided you don't panic and run the fuck away to begin with, which isn't entirely unlikely since you've probably spent the past while getting shot at by artillery and infantry. Oh, and even if you do stand firm, the guy next to you, the guy a bit further down the line or maybe his mate won't.
It's much more difficult to run away from a square. Your only real choice would be to run out of the square, which isn't very clever as that is where the scary horses can get you. So instead you stay in the square. Your mates stay in the square. You keep calm and fire volley after volley against the cavalry until they realise that they kind of have to run away. Another point is that horses are quite intelligent beasts and they're quite reluctant to charge into what they perceive to be a solid object, such as a square of people with pointy shit attached to their muskets.
Of course, a thousand men standing in a square is a comparatively easy target for someone who would like to shoot at you with a highly inaccurate firearm, no matter if it's a question of cannons or muskets. If we're talking about muskets, there is of course the added penalty of having deprived your formation of roughly 3/4 of your effective firepower, assuming that your enemy hasn't, of course, formed a square around your square. Squareception.