You can avoid the problem by keeping the ears dry most or all of the time by using ear plugs while swimming or showering.
If the ears are repeatedly getting wet, then you can use an over-the-counter product such as "Swimmer's Ear" or make your own (a mixture that is half plain white vinegar and half tap water). After a session of swimming or other water exposure, fill the ear canal, then tilt the head to let the solution drain; repeat in the other ear.
Sometimes an infection develops despite one's best efforts and an ear specialist needs to be seen. Generally debris in the ear canal needs to be cleaned out and a short course of antibiotics given to treat the infection.