When we eat food, it passes through the esophagus and enters the stomach. Here, the stomach acid begins to digest the food.
Now, the inner lining of the stomach is protected from the stomach acid with the help of mucus which is produced by special cells. However, the stomach acid can sometimes flow back into the esophagus, causing irritation, as there are no special cells to protect it. Hence, at the junction of esophagus and stomach, a band of muscles called lower esophageal sphincter or LES prevents this acid as well as chunks of food from flowing back.
However, when the LES abnormally relaxes or weakens the stomach acid is able to flow back into the esophagus, creating a burning sensation called heartburn. Heartburn may also be caused due to the consumption of alcohol, caffeine, chocolates, etc as they relax the LES. Remember that spicy foods and citrus fruits irritate the esophageal lining and increase the production of stomach acid, thus worsening heartburn.