Assuming there's no obvious cause, like accidental or behavioural lip or cheek biting/chewing, sports injuries, eating hot pizza and burning the gums or playing a brass/woodwind musical instrument (with embouchoure problems causing pressure ulcers), then stress +/- virus infections are likely causes and the usual general medical advice of avoiding stress, avoiding (.../reducing...) alcohol/smoking, eating well and possibly taking vitaminC/iron/multivitamin supplements may help.
If it's a recurrent thing without obvious causes then using Corsodyl (or other chlorhexidine mouthwash) in addition to general dental hygeine (brushing/flossing) may help in prevention.
Once an ulcer is there it's usually just a matter of waiting for it to heal - bonjela or local anaesthetic ('chloraseptic') type treatments may or may not help relieve pain but probably won't speed healing.