Firstly, it's important to make a distinction between "brain" and "nervous system". So-called "higher animals" tend to have centralised their nervous systems (i.e. brains) for convenience, simply because it's easier to wire a nervous system together if it's all in one place. With very few extremely simple exceptions, almost all animals have a nervous system of some kind.



Secondly, it's worth asking the question "what does your brain (or nervous system) actually do?" The simple answer is that they process information. At it's most basic, they take sensory information and turn it into a response. This is true for your brain too, although our brains are advanced enough that we can decide to do something without an external cue. However, even complicated behaviour like that is built on lots of tiny units (neurons) which work by listening for inputs and turning them into outputs.



You can do quite a lot without a brain, however - all you need is to be able to link an input to an output. For example, a barnacle might need to open up to feed and close up to avoid predators. Any stimulus that might mean "predator" pushes a button in the barnacles nervous system that makes it lock up. Nervous systems can be wired together to form quite complex behaviours. For example, there are more than a dozen muscles that need to activate in the right order for you to walk across a room; but you don't need to activate them individually - you just send and input saying "walk that at about this speed", and the circuit that controls walking handles the rest. Incidentally, even in people this is done outside the brain (in the spinal cord). In animals without brains the same principle applies - neural networks are designed to give you complex behaviours from relatively simple inputs.



Inputs don't need to be from the outside world either, an animal might be able to detect when it's hungry and activate the appropriate neurons which trigger feeding behaviours.