One way is to make sure that all your seed starting mix and the potting soil you use to transplant the seedlings is sterile. Once you have gnats already, you may want to read how to Get Rid of Gnats on Vegetable Seedlings.