But how does stress causes ulcers?
Stress/anxiety and other strong negative mental states can impact the functions of the GI tract's such as peristalsis and digestive efficiency of food.
This in turn sets up a negative feedback loop as things like nutrient absorption declines and toxin levels rise worsening the functionality of the tract. By toxins I also imply bacteria and viruses besides just 'regular' chemicals.
Erosion of various parts of the GI tract happens slowly and you get an ulcer. Ulcers in the stomach aren't the only place to see bleeding in the GI tract.