The infantry-cavalry balance has changed a lot over time. And back and forth.
In primitive warfare, the addition of a large animal gave the advantage to the cavalry. This changed during the times of the Greeks and Romans, who invented the phalanx and legion INFANTRY formations that had no cavalry counterparts.
By "stabilizing" riders in horses, the invention of the stirrup (fourth century A.D.) gave the advantage (using hand weapons) back to cavalry, which could now quickly form into large, heavy armored formations that even Roman infantry couldn't counter. It wasn't until the wide use of missile weapons (long bow, muskets, early rifles) that infantry could again fight on more or less equal terms again. The invention of "repeating" weapons put the advantage back decisively in favor of infantry (a cavalryman would manage a horse and a lance, but not a horse and a rifle simultaneously).
Even when infantry (mostly) had the advantage, cavalry had the advantage of speed and position. U.S. civil war generals considered cavalry mostly a form of "transportation," and often fought dismounted, with one man out of four holding horses for three other men. This was a disadvantage that sometimes, but not always outweighed the advantage of greater speed.
Cavalry could also get behind infantry, thereby fighting at an advantage. This was the case at Zama, where the cavalry won. On the other hand, the (Roman) infantry at Pharsalus was facing a FRONTAL cavalry attack--at a time when infantry had the advantage in such situations.
Even after the invention of the stirrup, which gave cavalry a shock power it had not had before, and before the invention of gunpowder, infantry in phalanx could sometimes defeat heavy cavalry on favorable ground or other conditions. Examples are the Flemish infantry in the battle of Coutrai and the Scottish schiltron developed by Wallace and used to devastating effect by Robert the Bruce. The most outstanding example of medieval infantry superiority, however, was that of the Swiss halberdiers, who dominated the battlefield against their Austrian and Burgundian enemies for nearly 200 years.