The central atom of a molecule is surrounded by pairs of electrons in its outer shell: shared pairs that form bonds, and lone pairs that do not. All of these pairs are negatively charged, so they repel one another.
The pairs settle where they are as far apart as possible. Two pairs point in opposite directions at 180°, three pairs spread out in a flat triangle at 120°, and four pairs point to the corners of a regular tetrahedron at 109.5°.
This idea is called valence shell electron pair repulsion, or VSEPR. Since the shape of a molecule is set by where its atoms sit, it can be predicted by counting the pairs around the central atom.
The carbon atom in methane, \mathrm{CH_4}, has four shared pairs and no lone pair. If the four pairs lay flat around the carbon atom, neighbouring pairs would be only 90° apart.
In three dimensions the pairs can get further apart. They point to the corners of a regular tetrahedron, with the carbon atom at the centre and an angle of 109.5° between any two bonds.
All four \mathrm{C{-}H} bonds have the same length, and every \mathrm{H{-}C{-}H} angle is 109.5°. A carbon atom with four single bonds has this tetrahedral arrangement in many other compounds as well.
The nitrogen atom in ammonia, \mathrm{NH_3}, also has four pairs of electrons around it: three shared pairs and one lone pair. The four pairs still point roughly to the corners of a tetrahedron.
The shape of a molecule describes only the positions of its atoms. With hydrogen atoms at three corners and the lone pair at the fourth, the atoms form a trigonal pyramid, with the nitrogen atom at the apex and the three hydrogen atoms at the base.
A lone pair is held by only one nucleus, so it stays closer to the nitrogen atom and repels the shared pairs more strongly than they repel one another. It pushes the \mathrm{N{-}H} bonds together and closes the \mathrm{H{-}N{-}H} angle to about 107°.
The oxygen atom in water, \mathrm{H_2O}, has two shared pairs and two lone pairs. Again the four pairs point roughly to the corners of a tetrahedron, but only two of the corners hold hydrogen atoms.
So the three atoms do not lie in a straight line. Water is a bent molecule, also described as V-shaped.
With two lone pairs repelling the shared pairs, the bonds are pushed closer together than in ammonia, and the \mathrm{H{-}O{-}H} angle is 104.5°. From methane to ammonia to water, the bond angle gets smaller as the number of lone pairs rises from none to two.
In carbon dioxide, \mathrm{CO_2}, the carbon atom forms a double bond with each oxygen atom and has no lone pair. The two shared pairs of a double bond lie between the same two atoms, so they act as a single group.
Two groups around a central atom point in opposite directions, so the three atoms lie in a straight line with an angle of 180°. Carbon dioxide is a linear molecule.
Water and carbon dioxide both consist of three atoms, yet one is bent and the other is straight. The difference comes from the lone pairs on the central atom: the oxygen atom in water has two, while the carbon atom in carbon dioxide has none.