According to Garrison Keillor, “Lutherans drink coffee as if it were the Third Sacrament.” This was not always the case. In the first half of the 18th century, many Germans looked askance at java, considering it a pernicious import. Additionally, some European princes forbade or heavily taxed coffee, in part to protect locally produced beverages from competition. Nonetheless, Leipzig boasted many lively coffeehouses. It was, after all, a college town, and higher education requires caffeine.
In one of those coffeehouses, Zimmerman's, a group of mostly student musicians known as the Collegium Musicum met every Friday to give informal concerts. Bach directed the group, and around 1732 he wrote the perfect piece for it: the Coffee Cantata.
The Christian History Institute has the whole story here.