If they affect the gums it can be because food/debris/bacteria have got stuck in the gums and the build-up of bacteria cause the abscess to form.
If the abscess is related to teeth, it is because decay has got to the nerve of the tooth and it has effectively ‘died’. As a result, the bacteria travel out of the tooth through the roots. The body tries to ensure the bacteria do not travel any further so therefore it creates the abscess. Though decay is the most common cause it can also be due to injury to teeth (trauma).
If your swelling relates to wisdom teeth, please see our Wisdom Teeth page