It's a fungus that you get from being barefoot in public showers. I used to leave my shower shoes in my room sometimes in the Army. I had a horrible case of athletes foot. Grew under my toenails and totally destroyed my big toenails. I used that prescription stuff to get rid of it, but my big toenails never recovered. They were about 1/4 inch thick
and hurt. So I had them removed.
Best way to prevent it is to wear shower shoes in places where fungus could be present, like public showers. You can also use those anti fungul powders on your feet. It helps to wear white socks too.