It all started billions of years ago when a certain arrangement of molecules acted as a catalyst to react two other molecules together that would not otherwise ordinarily have come together. The molecule it made was also a catalyst and made more of itself. This continued, using up the reactants in the environment. The number of these reactions would have given the molecules much more of an opportunity to make a mistake and copy wrong, remember their number would be growing exponentially. Mostly, these errors would just have been a dead end. But then it happened on a variation that made it more complex and better. This allowed even more variation in mutations, and then these families competed for chemicals. Not that there is any intelligence here, it's just a chemical reaction, complexity emerging from simpler systems. Stephen Wolfram showed how this is possible mathematically with his "cellular automata". Reaction became metabolism, they stumble upon more complex systems of heredity, an arms race develops, the result of which is the first cell. Maybe all these reactions happened in preformed cells in rock on the floor of the ocean, so that membranes would provide an advantage. The cells prevailed, and we are bunches of cells that have learned to live together in relative harmony, needing threat detection systems, and now possessing the ability to process information and abstract thought.