Broadly, religions evolve for two purposes, and arguably for a third purpose:
- To explain how
- To explain why
- To control
I'll use some examples, but please understand I'm trying to make my examples as simple as possible and they may not fully or accurately reflect the views of their respective religions.
Using Greek mythology: Greek gods served as supernatural explanations for many natural phenomenon which they may not yet have had scientific explanations for. Lightning and thunder was created by Zeus. The sun moved through the sky because Helios drove it in a chariot.
Using Christianity, in a Q&A format:
- a.) Q: Why do humans exist? A: God created them in his image
- b.) Q: Why do bad things happen? A: Adam and Eve ate the forbidden fruit (Note: I have grossly oversimplified for brevity)
- c.) Q: Why is it wrong to (steal/kill/cheat)? A: Because those behaviors are sins and forbidden by the Ten Commandments
In some cases, religions can arguably serve the purpose of controlling the population, perhaps to suit the purposes of the ruling structure. Using the excellent movie Max Max: Fury Road as an example: the primary antagonist Immortan Joe is a cruel dictator who treats most of the populace with indifference. However, he has built up (or allowed to build up) a religion centered around him as a deity. His 'war boy' soldiers treat him with fanatical reverence and will obey his every command without question. A mere glance of recognition from Immortan Joe is considered a great blessing, and they believe he has the power to grant them a place in Valhalla.
How to apply this
In creating a religion for a sci-fi setting, you generally want to think of important questions for which there is no known natural explanation. If you are describing a culture which is technologically advanced to the point of interstellar space travel, there are probably very few questions of "how" that haven't been answered by science. However, some questions of "why" can probably never be satisfactorily answered by science, so you still have some room to play there.
However, when you're writing a story (particularly a science fiction story), it's important to have details that are interesting or memorable rather than merely believable. A good way to handle religions is to come up with a belief system that will seem bizarre, amusing, or deplorable to a modern audience.
For example, you might have a story set a thousand years in the future, where scientists worship the Higgs boson like a god because they understand how to use it but have never figured out why it works.
Another way to amuse readers is to build a religion around some idea that is common knowledge today but has been forgotten in the future. For example, a group of humans become stranded on a distant planet and live there for generations. We encounter them worshiping a great metal god, which the reader understands to be a computer, because the culture has forgotten what a computer is.
Lastly, you can always come up with some society where religion is used as a means of control. Your crew encounters a primitive planet where everyone worships the god-king, but eventually the captain learns that the god-king is just an alien from a different planet who knows enough parlor magic to convince a more primitive species of his divinity. The religion on your colony moon is so pervasive that every citizen follows it without question, and eventually you learn this religion was created by the government several generations earlier because relying on religious beliefs to ensure good behavior is cheaper than having a police force, court system, and prison infrastructure.