Of course religion is connected to tribalism. [View all]
That's where it started. Humans tend to naturally form groups, starting with the family group and expanding as needed to survive. We don't do well as solo creatures, really.
As groups grow larger, though, some sort of order is needed to keep the group working together toward common goals. That order can be provided by a powerful chieftain who makes rules for however many people he can control. It's a precarious job, though, being chieftain of a tribe. Often, force is required to enforce whatever rules the chieftain makes, and surrounding oneself with strongmen often leads to one of them deciding that he should be the chieftain.
Religion can serve a similar purpose. Human individuals have lots of questions. It's our nature. We wonder about things. So, in most groups the size of a tribe, there's typically a clever person who is good at telling stories. Not only are stories entertaining, they can be used to explain stuff that isn't immediately obvious. So, a clever story-teller gains a different sort of power within a tribe. He or she explains the sunrise and the moon and the stars and all that stuff, through imaginative stories.
Smart chieftains see the effect of the story-teller and reward him or her for telling stories about why the rules are what the rules are. A bit of the choice parts of whatever animal is on the fire can go a long way toward getting the good stories told that help the chieftain maintain his position without having to kill too many of the tribe.
Then there are gods. They're also very useful. Typically, in basic cultures, there are lots of them. One for each inexplicable thing that needs explaining. The story-teller can easily come up with tales about those gods and how they came to empower whatever things need empowering. Gods for the sky, the sun, the moon, that pesky volcano over there, and the lion that ate your grandmother. It's all explained by powerful gods that have different priorities sometimes than the tribe has.
Sometimes a god can be credited with the power the chieftain needs, too. That's very helpful to the chieftain, who is often beset by jealousies and competition. As always, the story-teller can be counted on to fill in the important details.
And so it goes. As tribes encounter each other, they can have a battle over territory or sit down and combine their numbers, all the better to exploit the local resources. When tribes join together, though, their story-tellers may have different stories. Merging those is an important aspect of assembling a larger tribe. Often, a simplification of religion can help. Anyhow, some groups succeed and others don't. It depends.
In all cases, though, the religions developed by the story-tellers must reflect the general attitudes of the tribes and, of course, their chieftains. In larger groups, one chieftain generally ends up on the top of the heap, so there is some direction and one set of rules to follow. The wise and clever story-teller handles getting this across to the group.
And that's the essential story of religion. Gods are created by story-tellers to suit the needs of a particular group, its chieftain, and the surrounding natural environment. Over time, those things all change. New groups are enveloped by old groups, populations grow and shrink, and territories expand or diminish. The gods adapt, as they must, through new stories that derive from old stories.