When, where, why and how did the term head come to be used to describe quantities of cattle, animals, people, and so on?
It's ancient, from Old English (5th to 11th century). OED has
10. b. An individual animal, esp. a herd animal.
Usually with plural unchanged after a numeral or other quantifier.
OE Manumission, Bath (Corpus Cambr. 111) in J. Earle Hand-bk. Land-charters (1888) 268 Leofenoð..hæfð geboht hine & his ofspring ut æt Ælfsige..mid fif oran & mid xii heafdon sceapa.
My Old English isn't great, but I believe the last four words of that mean "with twelve head of sheep."
Sense 10a refers to people, and that has a similar citation from St Wulfstan (d. 1095):
OE Wulfstan Homily: Be Mistlican Gelimpan (Hatton 113) in A. S. Napier Wulfstan (1883) 170 Swa æt heafde peninc, swa æt heorðe peninc, swa æt sulhgange peninc.
As to why individuals should be counted as head, it would appear to follow from heads being easiest to count in a herd or crowd.