Experience is accumulated by kills. The game tracks the xp for each individual soldier, which is why smaller units (depleted) tend to gain xp a lot faster than full strength units.
This also means that your front usually is more experienced than your back for infantry, but the change in a battle is small because the front 2 ranks are few compared to the total ranks. Also, the front rank tends to die a lot faster due to doing all the fighting, thus reducing the total xp again.

XP is gained from participating in battles (the don't even have to fight at all, but helps) and from killing. There is little distinction between killing a peasant and a swordsman. But I think cavalry killing gives a bit more xp than infantry and routers give a bit less than holders.