In general there are two reasons.
First, what you eat affects how a drug is absorbed. Some drugs are absorbed best when taken with food, especially i they are fat soluble. Some cannot be absorbed and have a decreased effect when taken with food.
An extreme example is Fosamax - a drug your body almost cannot absorb. It must be taken first thing in the morning after fasting since bedtime. It must be taken with a full glass of water and you must stay upright during the next half hour for safety reasons. Having anything besides water, even a cup of coffee, means your body will not absorb he drug and you just wasted the pill as surely as if you had flushed it instead of swallowed it.
Second, The effects of the drug may depend upon food being eaten. Acarbose, short acting Insulins, and all Proton pump inhibitors. For the first two, they care closely connected with your body getting sugars from your food. For the pump inhibitors, they block the pumps, but only WHEN THE PUMPS TURN ON, so you want the medicine to be absorbed, then the pumps turned on and sabotaged when you eat.
Thirdly, some are just irritants and this is eased by taking it with food. The classic example being Ibuprofen, Naproxen, and other NSAIDs