The goal for most atoms (excluding hydrogen and helium) is to have eight electrons surrounding them. When we look at noble gasses, we see that they already have eight electrons and therefore don't form bonds. The few exceptions to this are with Xe and Kr who form a few compounds but only in the lab and not in nature.
For all other elements, they have to form compounds (i.e. form bonds) with other atoms to get their eight electrons. Some elements do this by transferring electrons as in ionic compounds. One atom gives up one or more electrons while other atom(s) accept the electrons.
For other elements, such as non-metals, they share electrons between them in a covalent bond and will arrange themselves such that they have eight electrons around each atom in the structure. As with everything in chemistry, there are exceptions to this but this is a general guideline. When you look at elements further down the periodic table (i.e. transition metals, lanthanides, actinides) their behavior is not as predictable as the main group elements but we still see some trends.