Whenever protons and electrons bump into each other, or atoms bump into each other, or molecules bump into each other, or prokaryotic cells bump into each other, or DNA molecules inside cells divide – whenever any of these things happen, they can happen in any of several ways, and random chance means that they do happen in lots of different ways. But some of these ways work better than others.
Say six protons bump into each other by chance. Their forces will push them apart again, so they won’t form an atom. But suppose one proton and one electron bump into each other. Then their forces will naturally pull them together, so they form a hydrogen atom.
Now say some of those hydrogen atoms happen to bump into a helium atom. Nothing will happen, because the helium atom is stable on its own and can’t bond with hydrogen. But if the hydrogen atom happens to bump into a carbon atom, they’ll naturally bond together to make a hydrocarbon molecule (like sugar or oil). A hydrocarbon molecule is chemically strong, or stable – it’s hard to break it apart. So more and more of the hydrogen and carbon atoms end up in hydrocarbon molecules.
Now suppose some of those hydrocarbon molecules happen to bump into some oxygen and nitrogen atoms – then they might happen to link up and become amino acids. Not all the hydrocarbons become amino acids; some stay just plain hydrocarbons. Some break down again into hydrogen and carbon atoms.
After some time, suppose gravity happens to pull a few of these amino acids floating around out in space down to a new planet, Earth. Here on Earth something new is happening – instead of the cold ice of space or the blazing heat of stars, there is warm water, heated by underwater volcanoes. The warm water makes different chemical reactions happen here, and some of the amino acids begin to bump into each other and stick together to make bigger and bigger molecules that we call RNA and lipids, and to make new molecules called proteins.