The evolution of multicellular animals depended on cells being able to sense and respond to other cells - to work together. Sponges, for example, filter food from the water they pump through the channels in their bodies. They can slowly inflate and constrict these channels to expel any sediment and prevent them clogging up. These movements are triggered when cells detect chemical messengers like glutamate or GABA, pumped out by other cells in the sponge. These chemicals play a similar role in our brains today (Journal of Experimental Biology, vol 213, p 2310).
Recent studies have shown that many of the components needed to transmit electrical signals, and to release and detect chemical signals, are found in single-celled organisms known as choanoflagellates. That is significant because ancient choanoflagellates are thought to have given rise to animals around 850 million years ago.
So almost from the start, the cells within early animals had the potential to communicate with each other using electrical pulses and chemical signals. From there, it was not a big leap for some cells to become specialised for carrying messages.
These nerve cells evolved long, wire-like extensions - axons - for carrying electrical signals over long distances. They still pass signals on to other cells by releasing chemicals such as glutamate, but they do so where they meet them, at synapses. That means the chemicals only have to diffuse across a tiny gap, greatly speeding things up. And so, very early on, the nervous system was born.