Coffee is decaffeinated before it is roasted. Producers moisten or steam green, unroasted beans and then pull the caffeine out with one of three main methods: an organic solvent such as ethyl acetate or methylene chloride, hot water filtered through activated carbon (the Swiss Water process), or high-pressure carbon dioxide that dissolves the caffeine. The beans are then dried and roasted as usual.
Solvent methods, the oldest and most common approach dating to the early 1900s, remove about 96% to 97% of the caffeine. The Swiss Water process removes about 94% to 96%, and the carbon dioxide method about 96% to 98%. Because none of these methods removes all of it, decaf coffee still contains a small amount of caffeine.
Image: Green coffee beans, photo by Michael Allen Smith, CC BY-SA 2.0, via Wikimedia Commons.