One answer, easily offered by a secular view of early humanities transition to an agricultural way of life, is that it was an invention of clever hustlers seeking community power. Convince the populace that you have a connection to an ineffable power, and therefore become powerful.
This argument applies to even pre-agricultural societies which were based on a hunter-gatherer/wandering system. This would be the archetypal tribal ‘shaman’, or holy person, a system yet extant in many indigenous peoples worldwide. The one who could read the signs, read the stars, talk to the dead and predict animal migration behavior. A position, which in post-ancient society would become the ‘priest’ or ‘priestess’.
Another answer would be a passing along of primitive beliefs derived by the common individual of the species which attributed the then unexplainable to a divine source.
Or it could have been based in a primal fear of the cessation of existence. This may even be present in the tentative evidence of burial practices amongst Neanderthals which purportedly speak to a belief in an afterlife, evidenced by the inclusion of everyday tools and items amongst a burial.
However, I would posit another theory based solely on speculation.
Take for instance, a very young child, only recently capable of speech, who is in an environment composed only of atheists. The first time that child saw lightning, would it ask if it was god doing that?
I think not. A child not introduced to the concept of a god, has no preconceived notion of it. Therefore, why would an ancient hominid suddenly turn to this conclusion for the unexplainable? If the god concept is not present in modern infants, then how/why did it occur in primitive hominids?
What did they perceive that acted as a catalyst for this leap in thought? How did they concoct such a seemingly ridiculous concept? Why did thunder or fire or an unusually rare sandstorm fuel the concept of the supernatural?
In this, I am speaking of peoples existing before the archeological record. Before writing as we know it, before complex art, before the idea of architecture as an enduring structure.
Why did our most primitive ancestors come up with a ‘god’?
Why would a species of hominid, at the time, who relied utterly and totally on the ability to rationally assess the world around them for their very survival turn to something they couldn’t measure or define?
Take lightning as a simple example.
I as a modern Human have a basic scientific understanding of what causes it, and what it is. However, after a bolt of nearby lightning, I can smell the ozone in the air. I can associate the presence of lighting with darkly colored clouds in the sky, the sound of thunder and rain.
How is it that an ancient primitive human, so finely attuned to the environment out of the sheer need for survival, would not perceive this as well? Why come up with a divine figure to explain lightning when they could easily connect it’s occurrence to dark clouds, thunder and rain, all of which would have been easily predicted through millennia of observation?
Why didn’t they attribute lightning to dark clouds in the sky and instead take up a belief that an invisible hominid in the sky was causing it?
I can easily understand the answer that they didn’t know where the dark clouds came from, why they produced lightning or why fired caused pain upon contact.
But why, WHY, did the choose a SPIRITUAL answer? Why not say ‘we just don’t know’?
Could it be that clever charlatan answer? A potential leader perceives the confusion of the community and decides to explain it by offering the spiritual as an answer? ‘Only Powers-That-Be that he/she could access?
But that again beggars the question. Where did THAT first charlatan get the idea of the divine? It would be easy to say that this person stood up and said to the community, “ I KNOW THE ANSWER! BUT YOU MUST MAKE ME LEADER OR I WON’T TELL!’
And of course, his/her answer was ‘there is an invisible power at work that guides and creates these things you do not understand’. And the cycle comes back upon itself.
Here is my answer as to how we as a hominid species came up with god.
We are Creative. And God is the Ultimate Creation. What work of Art down the long line of history can compare? God is Humanities very first work of Art. And obviously, the critics are abound.