Because some molecules have lower energy that the isolated atoms they are made of
Isolated atoms are usually stable if they don't interact with anything else. But, when things interact, you need to start thinking about what can happen and one way to do this is to understand the energy levels of the isolated atoms compared to the molecule.
In some cases when isolated atoms interact, nothing happens. Helium atoms, for example, just bounce off each other and no bonds are formed.
But two isolated hydrogen atoms, if they meet, will often produce a stable hydrogen molecule. When this happens, energy is released and we can measure how much. The hydrogen molecule, we say, has lower energy than two isolated hydrogen atoms. This we call a chemical bond.
But why is a molecule lower in energy than two isolated atoms? It is partially about electromagnetic forces (two positive protons attract two negative electrons in a hydrogen molecule). But knowing whether the net forces lead to a bond involves a lot of detailed calculations and you need a fair amount of quantum mechanics to even approximate the answer. The summary of the answer is that the amount of energy involved in the electromagnetic field of two isolated hydrogen atoms is a lot more than the energy involved in a hydrogen molecule. This isn't true for two helium atoms, which is why they keep to themselves and don't form bonds. But the details of those calculations are advanced.
A great deal of chemistry is about observing when and how bonds form and deriving general rules from observation that explain what happens without having to do those calculations (which are, in general, extremely hard). But the general rule is all about whether the amount of energy involved is lower with a bond than it is with isolated atoms.