Some animals are social animals. In the wild they would live in packs, herds, or groups. We domesticated some of these animals and we become their pack members or herd members. They want attention and since humans give it to them (and give them food also) they bond to us.
Not all animals that live in packs can be domesticated and behave too wild as they mature so we don’t keep them as pets or livestock.
Noting that when these animals which seem to enjoy our attention are not raised by humans (but live wild/feral) they don’t crave human attention at all and instead find companionship with other wild animals of their own kind. Wild horses will run from people, feral dog packs will even attack people. Feral cats will run away from people.