Surgery for a stomach ulcer is typically considered when other treatment options, such as medications and lifestyle changes, have not been effective in controlling symptoms or healing the ulcer. Surgery may also be recommended if there are complications like bleeding, perforation, or obstruction.