It blocks adenosine receptors in the brain.
As you go through the day, a chemical called adenosine builds up in your brain. It attaches to adenosine receptors on nerve cells and slows them down, which is a big part of why you feel sleepier the longer you've been awake. Sleep clears it away.
Caffeine has a very similar shape to adenosine. It slots into those same receptors without activating them, blocking adenosine from doing its job. Your brain cells keep firing, so you feel more alert. Caffeine also indirectly boosts dopamine and adrenaline.
Effects usually start within 15 to 45 minutes and can last for hours. Caffeine's half-life in adults is around 5 hours on average, so half of an afternoon coffee may still be active at bedtime.
When the caffeine wears off, the adenosine that's been waiting rushes in, which is why some people crash. Regular drinkers also grow extra adenosine receptors, building tolerance, which is why skipping coffee can bring headaches.