You know one way that atoms bond together, the Ionic bond, where two atoms oppositely charged attract each other. This is how very similar elements, or same elements, bond together. And they do this by sharing.
Remember, each element is try to get to exactly eight electrons on the outside shell. An elemental shell is an imaginary guideline showing how far from the nucleus the electrons are. In the first shell, the closest to the nucleus, only two electrons can fit, because being so close together, a third electron would be repelled away. All the outer ‘shells’ from that can hold eight electrons each, and that is the atom’s goal, to reach eight electrons in it’s outermost shell, unless if the outermost shell is the first one.
Let us say that two oxygen atoms float close to each other. Oxygen has a total of eight electrons, two filling up the first shell, and six in the one outside of that, meaning oxygen has two electrons to go before it reaches eight in the outermost shell. When the two atoms (let’s call them, for clarification, O#A and O#B) come almost in contact with each other, O#A begins to attract two of O#B’s electrons, but the electrons are still being attracted by O#B. So, because O#A won’t just let go of “his” new found electrons, O#B “decides” (these are just relative terms, by the way. Atoms don’t ‘decide’ things) to share them with O#A, so both atoms are being attracted to the electrons. In return though, O#B gets to share two of O#A’s electrons. So now, both atoms have exactly ten electrons attracted to them, with eight in the outermost shell.
Another thing that can happen is an oxygen atom comes close to a bunch of hydrogen atoms. Hydrogen has a total of one electron, and it needs one more to fill up the first shell. (also the outermost shell, as there are no electrons in the next ‘shell’) So, one of the hydrogen atoms attracts and shares one of oxygen’s electrons, and oxygen attracts and shares hydrogen’s atom with him. Oxygen now needs only one more electron, so it attracts another hydrogen atom, and they share electrons as well. So now we have one oxygen atom hooked on with two hydrogen atoms, written down as H2O. Looks familiar?