As has been stated already, fungal infections occur because of fungal overgrowth. It could happen in your nail, on it, or under it as well.
Fungi normally find it better to survive in environments that are warm and moist. So, if they find such an environment it is easy for them to add to multiply quickly. It is possible that the same fungi would cause a host of nail related infections such as jock itch, ringworm, and athlete’s foot, to name a few. Nail infections can happen because the fungi are already present in your body. You could also contract fungal infection if you come into contact with someone who is suffering from such an infection.