There certainly is no lack of books about religion, and ideas about how and why we humans have it. Stewart Guthrie has written, to me at least, a very persuasive account of the origin of religion.
Accounts for why we have religion can generally be divided into two groups- those of believers and skeptics. Believers of particular religions, of course, try to account for their religion in terms of a special revelation, and all others in terms of either ignorance or demonic influence. Unfortunately, these claims are special, not general, and there is no inter-subjective way of verifying them. Some liberal religious believers account for religion by essentially saying that specific practices and beliefs are irrelevant- they are all manifestations of religious experience. The problem is that mystical experiences are unique, autonomous, cannot be corroborated, and don't offer a way of knowing whether one experience is the same as or a different variation of another.
The skeptics' theories of religion are diverse, and make valuable points about perhaps why individuals choose religion and why it persists, but according to Guthrie, they don't explain the origin of religion very well.
* Wishful thinking- Popularized by Freud, the theory holds that people made up religion to satisfy their wishes, desires, and to derive comfort. But many religions have elements that would be hard to say are there for comfort and reassurance, such as malevolent spirits, hell, and jealous gods. Further we don't make up things in other areas purely for comfort- hungry people don't comfort themselves by telling themselves they've just eaten.
* Fear of death- Many have said that religion is primarily to overcome the natural human fear of death. This theory makes valuable points, but it can't account for the origin of religion. In ancient Greek religion and early Judaism, the doctrine of the afterlife was very vague. Further, in some early religions, the souls of the deceased were condemned to wander the earth forever, regardless of what they did or didn't do.
* Social glue- Durkheim and others have said that religion originated as a way to bind people together, just as national symbols do. While religions do do this, they also divide people. Guthrie notes that this theory fails to account for why people would use religion to form group cohesion in the first place.
* Intellectualist- Going back to Spinoza, this theory holds that religion is a result of anthropomorphism- attributing human properties to non-human things. People are in a world of uncertain objects, and so seek to understand the world through the use of a familiar model (ourselves). Another branch of the intellectualist theory is that we invent humanlike agents to reassure ourselves in a turbulent world. Guthrie ultimately thinks anthropomorphism plays the main factor, but neither of the explanations for why we do this are quite sufficient. First, we anthropomorphize familiar things (pets, for instance) as often as they do unfamiliar ones. Moreover, there are things (monsters in the closet) that we do not invent to reassure ourselves.
Guthrie gives a compelling account of religion. It is systemic anthropomorphism. Anthropomorphism arises out of animism- attributing animate features to inanimate things (trees, wind, etc.) We do this, Guthrie says, because of our perceptual uncertainty. Our knowledge of the world is not certain but interpretive. For instance, a door closing could be the result of wind, or of a person (perhaps a stranger); a shadow may be that of a tree or of a lurking figure- meanings don't reveal themselves to us, so we must construe them. So in assigning meanings, we use what is most significant to us. Since a potential predator would be more significant to our ancestors than pure nature, it would be a natural, safe bet to mistake a log floating in the river for a bear, rather than the reverse. Since humans are the most significant things to humans, we will naturally assign human-like attributes to objects and events in nature- we see storms as angry, trees as resisting us when we try to cut them down, etc. These are misapprehensions, but the impulse that gives rise to the misapprehensions is no mistake; it is an essential evolved strategy we have for understanding the world.