How can I prevent getting yellow and thick toenails?

Minimize risk. Wear proper fitting shoes, let feet air out when possible, avoid walking barefoot in locker rooms and public bathroom/shower. Treat athletes foot or skin fungus promptly. Do not leave nail polish on too long. Stay healthy.
Treat athlete's foot. Athlete's foot is a peeling skin rash on the feet, caused by a fungus. The fungus can spread to the toenails and cause thickened nails with powdery stuff under the nails. Treating a peeling foot rash early, with over-the-counter lotriminaf cream, may prevent the fungus from spreading. Once the toenails are involved, one must see a primary care doctor, dermatologist, or podiatrist for treatment.
Several ways. Good hygiene will help. Proper cutting and care for your toenails will help. Some people use an antifungal on a daily basis to help prevent fungal toenails.