Caffeine has numerous stimulant effects on the body. The primary mechanism which allows drinkers to stay awake for long periods of time is that it acts as a "nonselective antagonist of adenosine receptors".
In english, what this means is that there is a chemical called "adenosine" which our body uses to tell our brain that it's tired and really should go to sleep. Caffeine has a structure similar enough to adenosine that it can slip into the spots on the brain which are "listening" for adenosine. With caffeine filling these spots, the brain doesn't realize that your body is in need of sleep.
This is also part of how a dependency is formed. After a while of being fatigued, your brain gets suspicious and starts making more adenosine receptors to "listen" more intently for the signal to go to sleep. Once you have more adenosine receptors in your brain, then you become tired much more quickly unless you have caffeine to fill some of them. This downward-spiral is why drinkers can form such a strong dependency on caffeine.