Caffeine works mainly by blocking adenosine, a chemical that builds up during the day and binds to receptors that slow nerve activity and make you feel sleepy. Because caffeine has a similar shape, it fits into those same receptors without activating them, so the brain's "slow down" signal is muted and neurons fire more freely.
This blocking also indirectly boosts the release of excitatory chemicals like dopamine and norepinephrine, which helps explain the alertness and improved mood people feel after coffee. Over time, the brain often adds more adenosine receptors to compensate, which is one reason regular caffeine users can develop tolerance and feel withdrawal headaches when they stop.