American Academy of Dermatology (https://www.aad.org/public/diseases/contagious-skin-diseases/nail-fungus#causes) explains the cause of a nail fungal infection.
First of all, the infection is caused by a microscopic organism called fungi.
One way of getting a nail fungus is skin-to-skin contact with people who already have nail fungus or other fungal infection on their skin such as Athlete’s foot.
Another way of getting fungi in your nails is through objects. You can get nail fungus by walking barefoot in a warm and moist area such as public pool, or by sharing an infected nail clipper or shoes.
Nails get infected when the fungi get into the nail. Usually, the fungi get through
A small cut in the skin around the nail
Crack in the nail
A separation between the nail and nail bed
It is important to keep your nail healthy to prevent the fungi to get through your nail. When you have nail traumas or poor blood circulation, put extra caution to your nails.