You need to maintain a tight fit in the water to prevent water from traveling around the earbud and irritating you. Water is denser than air, so it is harder to get a good fit from a pair of earbuds under the water than it is to get a good fit from a pair of regular headphones. When you are trying out earbuds for use in the water, you should look for earbuds which give a tighter fit than you are used to. This will stop them from being disturbed when you are under the water.
One way in which you can easily keep earbuds in your ears is by using a swimming cap. Swimming caps are often not preferred by most water sports enthusiasts, but they are an effective way of keeping headphones secured. If you are having problems keeping the headphones secured in your ears, then a swimming cap is a good place to start.
If you do not want to wear a swim cap, then you need to look at the earbuds which best fit your ears. Before you buy your earphones online, it is worth visiting a swim store to try on a variety of swimming headphones to find the style which fits best.