The way that atoms bond together to form molecules has been a question asked since scientists came to the consensus that atoms do indeed exist. Work progressed rapidly after the turn of the 20th century from both theoretical and experimental breakthroughs. To keep the discussion easy to visualize, we shall consider only diatomic molecules, but the concepts are good for any number of atoms. One of the great advances was the development of the idea that chemical bonds can either be covalent, where each atom shares bonding electrons equally, or ionic, where one atom donates an electron to another atom entirely.
Actually, pure ionic bonds do not exist because all bonds have at least a little bit of covalent character. Pure covalent bonds are common, common examples being the nitrogen and oxygen of the atmosphere. There is a very cool way to predict where a particular bond falls in the covalent to ionic spectrum, and that is to use electronegativity values.