The systems vary wildly, from inductive loops at the side of the track placed near each signal (PZB), electrical contacts between the rails (Crocodile), to signals transmitted through the rails (ATB) or through leaky cables along the whole track (LZB). The installed base is obviously huge. There are often several not-entirely-compatible variants
within each country, mostly for cost reasons.
Oh yes, also patents.
Currently railways are installing the European ERTMS system on many tracks, usually as a second system so both trains using the local traditional system and the new ERTMS can ride there. However, it is quite complicated and it turns out not all trainside implementations talk correctly with all railside ones.