Feral cats – wild *felis domesticus * – don’t much care for being petted by people. However, they will form social attachments with each other. One way they show affection for each other is by washing each other (licking each other).
Socialized cats – cats conditioned to cohabitate with humans – generally get this way by being handled and petted as kittens. They come to associate humans with being petted, food, affection, attention, and suchlike. If humans continue to treat them in this manner, the cat will stay socialized forever. It’s not unheard of for socialized cats to go feral, but only after being ignored or abandoned.
Never having been a cat, I’m not sure what it feels like to have a furry coat and be petted by a human. I do know my wife really likes it when I carefully insert my fingers into her hair and caaaarefully scratch/massage her scalp. By the same token, I really like it when she does the same to my beard. For that matter, my wife and I pet on each other quite a bit, by way of being affectionate and providing each other with love and attention, but that may be TMI, so I’ll stop there, and change the subject back to cats.
Any cat likes being the center of attention, in a non-threatening kind of way. Socialized cats are used to being petted by humans, being shown affection, and so on, and certainly some cats would get the idea that if one human will pet you, perhaps others can be persuaded.
As to why people pet cats… well… cats are cute. Cats are soft and furry. Petting cats is fun, if you’re so inclined in that direction, particularly when the cat in question is affectionate back (had one cat once that would wash members of the family. My roommate actually held still once to see how far the cat would go. Durn cat literally washed her from head to foot, while she lay there on the couch. Very patiently.)
Y’r a cat person, or you’re not.
As to how it began: well, from what I understand, around the time agriculture was really getting started, humans faced the problem of storing leftover food. This is where the vermin came in. Cats like to eat vermin, and even if they don’t eat them, they tend to kill them for fun, and generally just make 'em keep their distance, as well as keeping the population down.
Our neolithic ancestors, being no dummies, did not bother the proto-cats who moved into the neighborhood.
As to how they got socialized… well… not sure. I suspect someone found a litter of kittens, and raised 'em, and kept ‘em around, socialized them to human contact, and bred them. Certainly this would be a profitable sideline in an agricultural community, and pretty soon, domestic cats were a happenin’ thing.