There are many causes of a burst ear drum, some are easily avoidable, some aren't.
Otitis media (middle ear infection) is a common cause which can be prevented in some situations. Otitis media may spread from otitis externa (swimmer's ear) which can often be avoided by wearing ear plugs for swimming.
Another cause of a perforated ear drum is exposure to high velocity pressure waves such as when a bomb explodes or a lorry tyre bursts near your ear. Again, the problem could be prevented in these situations if ear plugs are worn. Unfortunately these events are usually a surprise and so the ear plugs are not usually in place when you need them. The ear drum can also perforate if a sharp object such as a stick is pushed down the ear canal. Perforations usually heal easily in between a week and 6 weeks depending on the size and type of hole. It is worth getting a doctor to check for healing before getting the ears wet as it is possible this could cause damage. Ear plugs can help protect the middle ear when washing the hair. They are extremely comfortable and last for many years, despite regular heavy use.
Sometimes a ruptured tympanic membrane doesn't heal spontaneously. A surgical repair may be required; a tympanoplasty. A tiny patch is sewn over the hole.