Well I'm sure that when our brains grew enough and became more proficient at analysing sensory input data and started looking at the stars and imagining what they were it could quite naturally morph into some type of deity worship based on what was going on in the unconscious mind, and made it to consciousness. This no doubt happened wide spread among our speicies before we started settling down and building villigages, cities and having rulers appointed by the gods, even becoming gods.
These ideas helped the rulling class to keep order, and elabrorate calenders eventually came into being telling when to plant crops and so forth from methods envolving the sun and stars(which they thought were gods as they fortold so many inportant things nessecarry for survival.