There seems to be a pattern in history, starting with bronze age chariots, and ending with the medieval heavy cavalry, where warfare starts to center around highly specialized horse-based soldiers. Then all of a sudden, everybody starts carrying spears, horses can't get close enough to do any serious damage, and its back to infantry warfare. My question is: why did any society ever stop fielding phalanx/spearmen/bayonets long enough for cavalry to become a thing?
That's an overly simplistic way of thinking about cavalry. Battles usually didn't focus on cavalry charges (though they often were used as shock operations during battles) but massed infantry holding terrain. Cavalry was more often use for scouting, rapid flanking actions, harassing rear guard units, and rapid positioning of troops where they were needed.
In all those scenarios, they're not likely to meet the massed pikes of the heavy infantry, massed pikes that developed in an arms race not so much to counter cavalry (which wasn't the main concern for heavy infantry) but to give them more reach than the massed pikes of the opposing army. That's how in ancient Greece and Macedonia their pikes over time reached such incredible and to be honest utterly impractical lengths, each time one city state or country increased the length of theirs, everyone else rushed to outdo them and gain an advantage. That they also helped against the rare cavalry charge was a mere bonus.
The idea of the massed cavalry charge I think started much later, in medieval Europe, where it was mainly used against poorly trained masses of poorly trained light conscript infantry, armed with often nothing more than clubs and hatchets. A cavalry charge against that, with the cavalry men armed with sabres and lances is devastating. Not only does it cause serious casualties, it also shatters the unit and destroys its morale, potentially causing a major break in their lines.
That kind of charge was only defeated with the large scale availability of the musket.
Cavalry is far more mobile than the alternative of the time - foot soldiers.
Yes, spears can be an effective counter to a calvary charge, but only if the cavalry chooses to charge into a spear armed formation. But, the cavalry can do something smart, like use its high mobility to attack from several directions, or attack a supply line or headquarters that is too far away for spear armed foot soldiers to reach in time to stop the attack. Spears are of no use against cavalry, if they are not where the cavalry is attacking, and can't be moved there in time to counter the attack.
Many centuries later, when the spears became fortifications along an expected route of attack, and the cavalry became mechanized troops, aircraft, and armored vehicles, the effectiveness of mobility was demonstrated when Germany defeated France in six weeks, with a smaller force, by bypassing the fortifications and hitting the soft areas far quicker than the French thought they could.
I always thought of it as a kind of arms race, where you learn to counter one enemy and become very good in this way of combat till you everyone believes this to be the only way to fight effectively. Now, someone finds a way to counter your way of combat, that becomes all the rage for a few years till it's weakness is found and it is countered, and so on, until you are back at the starting point. Cavalry is countered by phalanx - which in turn is countered by a mass of mobile, lighter troops - which is countered by cavalry.
Naturally, that is not the whole truth. Especially because it is not truely a circle, i.e. cavalry does not equal cavalry, e.g. one time, it may be mounted archers, the next time they are heavily armored knights or light skirmishers. My point is: an army reflects the enemy it fights and when your enemy is all infantry, you do not need to be prepared to combat mounted enemies.