The key is reproduction acted on by selection. Follows is an explanation of evolution. This is basically the formulation that Asimov used.
Imagine a chemical process that, entirely through chance, can make copies of itself. That is, the first step of abiogenesis is complete. There are a number of candidates for what the first replicators were, but the evidence is not retained since these chemicals were very fragile. And they tended to get consumed by many other chemical processes going on around them.
Now suppose that some inheritable feature of the copying organisms determines the characteristics of the copies. The copies look and behave like their parents most of the time, including making copies. So copying also copies the "instructions for copying."
Now imagine there is a small error rate in the copy process. This results in some variations in the copied items. This is true of DNA and RNA, for example.
Now imagine that, purely by accident, some particular variation is a little better at copying itself in the conditions it happens to be in. That variation will tend to dominate, in that particular set of conditions, and become more common relative to its neighbors. This is true whether it is behavior of the organism, or efficiency of chemical process, or sensitivity to important conditions around it, or any of several other possibilities.
From this new population there will similarly be variation. This can act as a scaffold. Each variation allows a new population of variations. The most efficient copying form becomes dominant and forms a new population. And so on.
So each time this happens, the newly dominant form is a little better adapted to the particular surroundings. Thus the plaint of the puddle: There must be a god, or how could the indentation under me fit my puddle shape so well? On evolutionary time scales, the puddle is adapting to the dent, not the dent to the puddle.
Now, consider that along the way, something has to happen. Many of the individual organisms must reproduce. If they don't then they don't leave offspring, and disappear. As the old joke goes, I come from a long line of people that had children. Indeed, if there is a characteristic of a particular variation that reduces its chance to reproduce, then that characteristic will get filtered out. It's ok if some don't reproduce so long as there are many that do, sufficient to keep up the numbers.
So, for each variation, there is a filter that picks out features that mean "behaves so as to reproduce." This feature will be fixed in by every generation. In evolutionary terms, being numerous is a powerful factor.
For bacteria these are not really desires. They are chemical conditions that cause various responses under various conditions. Lots of food will mean lots of reproduction, for example. These will all be chemical signals and conditions, all very much without any input from any sort of thought process. For creatures with brains it definitely becomes desires.
Note that this process can't predict the future. It cannot form long-term goals. If, for example, an organism were to adapt to a finite resource, it could find itself in a dead end regardless of how efficient it was. Maybe it adapts to some chemical that exists in a small deposit in a particular niche in the bottom of the ocean. When that chemical is gone, so is the organism.
Note that it is relative numbers that are important. The ancestors of snakes gave up their legs to adapt to a particular niche. They out competed their immediate neighbors. They did not become universally better at competing.
Note that there are often many compromises. For example, larger eyes may enable an organism to better adapt to a poorly lit location. But larger eyes use more energy, and are larger targets for injury.
Note that a big part of the environment an organism adapts to is the other organisms. When they change the organism may find it is suddenly very poorly adapted.
So the driving force is selection acting on reproduction. Those organisms that don't reproduce as well will get out competed. Those that don't work at it very actively, also get out competed.