Consciousness arose on earth as a result of very simple organisms mutating their genes in such a way that allowed for an inside and outside partition to be erected between the organism and its environment, for example the boundary of a cell as its membrane, basically an exclusionary zone; originally there were very simple free floating RNA strands drawing amino acids and heat from the primordial soup around them to further the most basic replication of the strand, but soon strands started to code for basis “proteins” that allowed for some degree of protection as isolation from the soup and from other strands or from environmental hazards, namely an RNA strand that has mutated randomly to code for the creation of some protein molecules around itself was able to survive a bit more easily than strands without that. This was the beginning of evolution as natural selection, and over time RNA strands of amino acids became longer and coded for more complex proteins until basically a “cell” formed. The first cell was able to isolate itself from the surroundings via a cell membrane wall, which wall was also porous in the right way to allow needed amino acids inside for replication of the RNA strand. Eventually cells became able to provide for their own energy by converting other proteins taken from the environment (other RNA proto-cells) into energy as breaking apart those proteins to extract the amino acids out of which they are made, and using released chemical energy from these molecular processes as cellular energy to power an increasingly complex cellular protein machinery.
Later, small protein structures evolved to allow the cell to move around via little tail-like protrusions. This meant that the cell could divert some of its internal chemical energy into that tail causing it to spasm around, causing the cell to move randomly, which increased mobility and thus allowed further increase to survival potential since this cell was now able to encounter more free amino acids and more proteins from its environment than were other cells without such tails and thus basically just sat there helplessly in the currents of the soup.
From here, natural selection took these proto-cells to multicellular organisms and eventually to the first sea life. At some point a photosensitive cell evolved, which is just a cell that was able to respond inside itself to a differential of photons impacting the chemical machinery in that cell, and natural selection selected this mutation because now the organism could respond to the presence or absence of light. And so on and so on, until a whole cellular molecular machinery of sensorium had evolved. Look up sometime the molecular processes involved in your own eyesight, you will be amazed how utterly complex and intricate these processes are.
Eventually you had organisms evolving a central area in the multi-cell structure for where inputs from the sensorium converged, which allowed for different sense inputs to affect each other directly, leading to de facto coordination of inputs, leading to increased ability of the organism to direct its own movements to move toward what was good for it and avoid what was bad for it: this architecture of moving toward or away is what we call instincts, which rigidified over a long time as natural selection selected certain configurations of the sensoriums that happened to benefit the organism itself, by making it sensitive to the presence of nutrients and forming a connection between the flailing of the tail and the senses that could detect nutrients, again thanks to the convergence of inputs and outputs that would later become the nervous system.
Organisms that got eaten or that ate things unhealthy to it simply died off as their RNA structures did not live long enough to reproduce, so the evolution was being refined both ways, positively and negatively, until organisms were highly specialized to thrive in their environments.
Now, “consciousness” is basically just this same system of the evolving environmentally-sensitive organism carried forward to the nth power; eventually the instinct pathways were extensive enough and lengthy (internally spatial) enough qua chemical molecular chain-reaction processes that “time” appears in them, namely the time it takes from the impact of a stimulus to the reaction it ultimately causes from the organism was sufficient to produce an inner sense of time passing, which simply means that those instinct pathways were long and complex enough to tie in other instincts and biological processes, which meant that instinct became “self-responsive” and able to mediate its own interactions by siphoning off some of the chemical impetus from the chemical chain-reaction into other processes. This allowed for an “inner world” to start to appear as a kind of sense of the organism to itself, its ability to start to respond internally to its own responses… eventually this created mammal life and then humans.
Our own human consciousness is simply this mammal structure taken to the nth degree, and by various lucky genetic mutations we developed the capacity for digital (concrete repeatable phonemes) speech, for a weaker jaw muscle which allowed the cranium to soften and expand to allow a larger brain, and opposable thumbs that allow hugely increased controlled interaction with the environment. Long story short, early human ancestors evolved from primates as all those changes converged and we developed the basic capacity for, as I already called it, using our physical material organism and brains to respond to non-physical, non-material conditions such as the fact of something, or the deeper meaning of something, or the non-immediate of something (predicting a future state before it obtains). This is basically what our “self-consciousness” is now, a very complex machine of material processes that fills itself in with non-material things (“ideas”, etc.) in order to self-reprogram ourselves and each other over time and as what we call thought, emotions, and culture. This all gives us access to far more truth than any other animal can access. It’s what allowed us to make acute observations and record these in external texts and stories able to survive our own death and to spread insights to others who can read those texts or stories, thereby creating a huge external universe of ideas that grows over time. It is by virtue of that universe of ideas, so called, that infants today are educated and able to become human, otherwise without it they would just be feral, like feral kids raised in isolation from other people; such kids don’t become human, because they lack access to the universe or collected ideas and programmed response-patterns that make us human.
Consciousness is just a very complex system of reflex and response, and in humans this includes very complex self-reflexivity and self-responsiveness as well.
As for souls or consciousness outside the body, I can’t speak to that.