For example, consider the element hydrogen. Hydrogen atoms have one electron each. When two hydrogen atoms come together, they can share their electrons to create a molecule of hydrogen gas (H2). In this molecule, each hydrogen atom has two electrons, which is a more stable configuration than having one unpaired electron.
The same principle applies to all other elements. Atoms will bond together in order to achieve a more stable electron configuration. This is why we see a wide variety of compounds in the world around us, each with its own unique properties.