We actually have a pretty good outline of the answer!
Bear with me. Let’s start with the most basic nervous system. Something like a starfish, or even a jellyfish. What is the point of this nervous system? Why would it evolve?
Basically, the nervous system is a way for an organism to *process sensory inputs,” and output a fitting behavior. All organisms do this, even cells. But a cell may only be able to measure a few kinds of inputs, like “hot here, cold here… move towards cold.” Plants grow towards light; they don’t have nervous systems. So the concept isn’t magical or anything; it’s everywhere already.
The advantage of a nervous system over something like a plant is: (1) it’s faster, can more directly guide responses to stimuli, and (2) it can internalize more diverse kinds of stimuli. Even basic animals like starfish can recognize stimuli like heat, light, and touch.
And this gets into a central idea about nervous systems and brains that is so easy to overlook. The brain categorizes information about the outside world. The evolutionary purpose of the brain is to take incoming stimuli—light, heat, touch, sound—and chop it up into categories. For primitive animals, those categories are going to be very simple. “Bright/dark. Hot/cold. Loud/quiet. Hard/soft.”
But then, it’s not hard to see how a brain that can subdivide more categories offers better evolutionary advantages. If a starfish can recognize not just hard/soft, but brittle/smooth/fleshy, it’s going to be better at finding tasty mussels to eat. If a jellyfish can recognize not just “bright/dark,” but shapes of reflected light—with certain shapes being hardwired to “flee” or “eat” behavioral response—it can survive better.
So here we have these brains that are pressured, by evolution, to come up with more and more precise ways to divide up and categorize information about the outside world, with each category wired to a behavioral response. And it’s not just the outside world either. Stimuli can include goings-on inside the animal body. Out of energy? The brain categorizes this as the sensation of hunger. Hunger makes your body try to find food. Out of water? Thirst. Body, find water.
When we get to vertebrates (and maybe even before then—obviously some invertebrates like octopuses are conscious), brains have evolved the ability to categorize all sorts of stimuli: shapes and colors of light reflections, a whole gradient of heat, varied sound patterns (or, related, the lateral line in fish), hunger, thirst, pain, desire to reproduce. If there is a pattern out there, or in your body, that is evolutionary advantages for your body to respond to, the brain has an evolutionary pressure to come up with some way to categorize it.
So that’s where sensation comes from. But consciousness is a little trickier. Where does the sensation of “I” come from? Of some sort of internal narration, a train of thought?
The answer is that consciousness is the brain’s way of categorizing its own activity. It’s a feedback loop, like when you point a videocamera at a TV that it’s hooked up to. Your sense of identity is your brain’s way of categorizing, and behaviorally responding to fluctuations in its own activity. It is a great example of an emergent phenomenon.
At least, that is what Douglas Hoffstader argues in his book, I Am A Strange Loop. But it totally convinced me, and if you’re interested in this question, I’d highly recommend it.