There are two basic ways that elements can combine to form Compounds, and they all relate to the number of electrons in the outer electron shells of the atoms.
1. Metals typically have few electrons in their outer shells called valence electrons. There is a relatively weak attraction between these valence electrons and the nucleus of the atom. Non-metals typically have many electrons in their outershells, but not enough to complete the shell. They have a relatively strong attraction between the valence electrons and the valence electrons. When a metal atom approaches a non-metal atom, the valence electrons of the metal get pulled into the gaps in the non-metal's outer electron shell. This gives the metal atom an overall positive electric charge, and the non-metal an overall negative charge. An electron that has an electric charge is called an ion. If the ion's charge is positive it is called a cation. If the ion's charge is negative it is called an anion. Opposite charges attract causing the cation and anion to hold together in an ionic bond.
2. When non-metals only have non-metals around, they tend to share the valence electrons so that all the atoms have complete outer electron shells. The atoms stay together to maintain these complete shells, and this forms what is called a covalent bond.
There is also a third type of bond called a metallic bond, but it works very much like a covalent bond, just between metal atoms instead of non-metals.