Caffeine is almost always created as a byproduct of the decaffeination process.
It IS possible to synthesize caffeine. The most popular lab synthesis (1,3-dimethylurea and ethyl 2-cyanoacetate) requires multiple steps, and generally more steps means more expensive.
Luckily, there is a huge demand for decaffeinated coffee. The bonus is exactly as you expected; these processes generate both decaf coffee and caffeine as a by-product, that can then be repackaged or sold in other products.
Historically, decaffeination was done with organic solvent extraction (chloroform and dichloromethane being really common), but today is is usually done with the swiss water process (I suspect this is what ends up in pills and added caffeine consumer goods) or supercritical fluid extraction (which is more energy intensive, but can lead to a higher quality product that is often purchased by laboratories and scientific researchers who use it as part of their experiments).