There are two broad groups of theories about the origin of religion:
Faith-based theories: According to David Barrett et al, editors of the "World Christian Encyclopedia: A comparative survey of churches and religions - AD 30 to 2200," there are 19 major world religions which are subdivided into a total of 270 large religious groups, and many unique faith groups. Among this great religious diversity, there are probably hundreds of different religious creation stories which describe how humans, other species of life, the Earth, and the rest of the universe came to be. Many of these stories describe the origins of their particular religion. It was typically based on revelation from one or more deities -- mainly gods and goddesses.
Secular-based theories: Anthropologists, evolutionary biologists, and other researchers have reached a near consensus that humans of the species homo sapiens evolved from a species of proto-humans who originated somewhere in Africa. (This statement probably upsets any white supremacists who are reading this essay. That can't be helped; scientists consider the evidence to be conclusive; ultimately, we are all descended from Africans.) These proto-humans walked upright, and had an opposing thumb and little finger. Their internal brain structure represented a major advance over those of previous animals in terms of its flexibility, its ability to reason, and its ability to plan for the future. This gave proto-humans an improved ability to pass on their accumulated knowledge to their descendents, to form more advanced societies, and ultimately to create religions.
Nobody knows with accuracy how the first religions evolved. By the time that writing had developed, many religions had been in place for many millennia and the details of their origins had been forgotten. However, there is speculation that the first religions were a response to human fear. They were created to give people a feeling of security in an insecure world, and a feeling of control over the environment where there was little control.
The developing abilities of proto-humans were a double-edge sword:
- On the one hand, they aided their chances of surviving in a cruel and unpredictable world. They helped each successive generation of proto-humans to build upon the knowledge base of their ancestors.
- This increased mental ability led to a terrifying piece of knowledge: personal mortality. For the first time, individual animals on earth became aware that their life was transient; they would die at some point in their future. This knowledge produced an intolerable emotional drain.
- What controlled the seasonal cycles of nature -- the daily motion of the sun; the motion of the stars, the passing of the seasons, etc.
- What controlled their environment -- what or who caused floods, rains, dry spells, storms, etc?
- What controls fertility -- of the tribe, its domesticated animals, and its crops.
- What system of morality is needed to best promote the stability of the tribe?
- And above all: what happens to a person after they die?
- The first religious belief system,
- The first priesthood,
- The first set of rituals to appease the Goddess,
- Other rituals to control fertility and other aspects of the environment,
- A set of behavioral expectations for members of the tribe, and
- A set of moral truths to govern human behavior.
Unfortunately, because these belief systems were based on hunches, the various religions which developed in different areas of the world were all different. Their teachings were in conflict with each other. Because the followers of most religions considered their beliefs to be derived directly from God, they cannot be easily changed. Thus, inter-religious compromise is difficult or impossible. Also, because religious texts are often ambiguous, divisions developed within religions. Different denominations, schools, or traditions have derived different meanings from the same religious texts. Thus were laid the foundations for millennia of inter-religious and intra-religious conflict.