Caffeine is obtained commercially as a by-product of the decaffeination process which leads to the question how is coffee decaffeinated?

Organic solvent extraction

Toxic solvents were used in early decaffeination efforts, including benzene and chloroform. Dichloromethane, CH2Cl2, became the solvent of choice in the early 1970's because of its lower toxicity and its ability to selectively dissolve caffeine without carrying off sugars, peptides, and other flavor ingredients. However, evidence then suggested that CH2Cl2 might be carcinogenic and as a result its use was sharply reduced.

Two non toxic solvents are now used only the one is discussed here.

Supercritical fluid carbon dioxide extraction

What is a super critical fluid?

When a sealed vial containing both gaseous and liquid carbon dioxide under high pressure is heated, the liquid density drops while the gas density rises. If the density of the liquid and the density of the gas become identical. The meniscus between the liquid and gas phases vanishes and the carbon dioxide becomes a supercritical fluid, which has both gas and liquid properties. The point at which this occurs is called the critical point. The fluid fills the container like a gas, but can dissolve substances like a liquid.
Super critical fluid carbon dioxide is a very good solvent for organic substances such as caffeine. The extraction is simple the fluid is forced deep into green coffee beans its gaseous properties enable it to do this it then dissolve a vast majority of the caffeine.

The caffeine laden carbon dioxide is then sprayed with high pressure water the caffeine is then obtained via a variety of methods such as reverse osmosis.