Fungus grows under the nails due to infections of fungi.
The fungi can thrive in environments that are warm and moist which is how you usually get fungus growth under your nails.
When washing your hands make sure you dry your hands with a towel really good or use one of those hot air hand dryers which is better at getting your hands dry and the heat should help to kill off bacteria.