Are we talking single player or multiplayer here? Because it's generally easier to catch and take out the AI's cav than it is to take out that of another human player.
As Holy Cow says spearmen are a decent counter and the heavier spear units can even withstand cavalry charges without too many casualties. They're not the best damage-dealers though, I'd mostly use them to tie down the enemy and use another unit to provide the extra damage by flanking them, so using the hammer and anvil against the cavalry. This could be your own cav charging in or an armour piercing melee unit. Units with high damage and the armour piercing trait can absolutely obliterate even the thoughest cavalry. The problem with this tactic is that it's probably very hard to accomplish against a human player unless he forgets his own cavalry and it requires you to use several of your own units to take out 1 of the opponent, leaving you outnumbered elsewhere. It's also hard to catch cavalry with infantry so an initial cavalry attack from you followed by your spearmen charging in and your own cavalry pulling out before sustaining too much damage would be the trick.
Pikes are of course an amazing counter to cavalry but they're slow and hard to manouevre so they don't lend themselves too much to attacking play and chasing enemy cavalry. You can throw them in when the enemy is tied down or hope they're dumb enough to attack you head on though.
Another method would be missiles, knights provide for a big target and if you fire enough arrows at them they'll die eventually. Crossbows and javelins are very effective, due to their high damage and armour piercing but they have their limitations. Crossbows are slow and can't fire in an arc so you need a clear shot and can't just pop them behind your main line and javelins are close range and have low ammo. Both are incredibly deadly if you manage to flank the enemy though.
Regular archers can pull off the arced shot and the elite ones have a good range, some like the longbows might even have armour piercing in vanilla, I can't recall.
Horse archers are another counter but they're a pain to manage, certainly against a decent human player I'd think, and they generally don't pack enough of a punch to take out the heaviest of cavalry. You could sacrifice some to distract the enemy cavalry though, taking them far away from the main battle and forcing your opponent to spend a lot of time on them, distracting him from the main fight.
The main point fighting enemy cavalry is to limit the impact of their charge as much as possible. Cavalry isn't great in prolonged melee, it's the charge that wins battles for them and they'll lose a lot of men when kept there (unless they're Byzantine bodyguards, those are sometimes almost impossible to kill
). So to stop the charge either use your own cavalry or infantry that can take a punch like pikes, heavier spears or units with the shieldwall ability. Head to head cav charges are fairly ineffective and the infantry units should be able to survive a cavalry charge without taking too many casualties.