My mom tried to teach us ways to stay healthy, but I got ear infection. Why is that?

Depends. The skin of the ear canal is susceptable to cracking if water remains too long, opening the skin to invasion by germs or fungus. Water removal after swimming decreases that risk. The middle ear occasionally retains mucous if the eustacian tube blocks (cold/irritants/allergy) & germs invade from the throat. Middle ear infections are most common <2yo but can occur in anyone.
The two types... ...Of the most common ear infections are either complications of a runny nose (otitis media), or "swimmer's ear" (otitis externa).