By around two billion years ago, some eukaryote cells were able to sense changes in their surroundings and react to them. For instance, some cells had eye spots, which could sense sunlight. When they sensed sunlight, they sent energy to move the flagellum, which pushed the cell toward the light.
The earliest creatures with more than one cell, sponges, have no nervous system at all. But in hydras, even though they were only two cells thick, between those two layers of cells there was a nerve net, which was made of specialized cells that could pass messages from one cell to another, still using electrical energy as before. Hydras and jellyfish didn't have any brain, so the messages went straight from sensory cells that got information to the muscle cells that reacted. Some molluscs, like clams, also operated this way, without any central brain.
Flatworms, about 550 million years ago, were the first creatures to have the beginnings of a brain. Because they always had one end (the head) forward as they moved, flatworms evolved to keep all of their sense organs at that end so they could know what they were moving into. Flatworms have eyespots. With all the sense organs in the head, flatworms needed more nerves there to coordinate information. Along their bodies, flatworms had two long nerves connected together with smaller nerves like a ladder, with bilateral symmetry. This kept the two sides of their body coordinated with each other. These nerves were made of neurons. Neurons are specialized cells that are very long and thin. They are especially good at sending information from one end of the cell to the other. Thanks to the ladder of neurons, flatworms could coordinate muscle cells on both sides of their body to inch themselves along.
Beginning around 542 million years ago, the arthropods (insects and crabs) had small brains, with one nerve cord running all along their body. Inside each segment of their body, there was another small center, or ganglion, that controlled that part of the body. In their heads, these arthropods also had an early form of eyes.
During the Cambrian period, about 500 million years ago, the first animals developed spinal cords. These animals were like eels, still living in the water. These early spinal cords were hollow tubes of neurons, like a flexible drinking straw. Gradually these neural tubes evolved into brains and spinal cords.
By around 420 million years ago, fish were developing new parts of their brain that made them a lot smarter than earlier animals. The evolution of the jaw and teeth allowed fish to chew their food and get more energy out of it, so they could afford to use a lot of energy to run their brains. Also around this time, neurons became more specialized, so that some of them carried pain messages to the brain, while others carried hunger, thirst, smell, touch, itching, or other messages, and different neurons carried messages in the other direction, from the brain to the muscles.