Question: How can a substance be both an acid and a base? I know water and ammonia can be both, but how is that possible?
A substance is an acid if it gives off H+ ions and a base if it absorbs H+ ions. When water acts as an acid, it loses H+ to form the hydroxide ion, and when it acts as a base it picks up H+ ions to form H3O+. Ammonia is the same way: As an acid it forms NH2-, as a base it forms NH4+.
Of course, this doesn't explain why it happens. Generally, we see compounds behaving as both acids and bases for two different reasons. The first is that the central atom in the molecule is somewhat electronegative and has some extra lone pairs. The lone pairs allow the atom to pick up H+ ions (when it acts as a base) and the electronegativity stabilizes the negative charge resulting from losing H+ (when it acts as an acid). This is why both ammonia and water are able to behave as both an acid and a base.
The second reason is that some molecules have one functional group that is acidic and another that is basic. A classic example of this is an amino acid, where you see a basic amine group on one end of the molecule and a carboxylic acid group on the other end.