Usually because the substrate is too wet, constantly.
Let it dry out, they will soon die.
If you go to the menu on the left side of this page and click on "journal", then scroll down a fair way, Grow Diaries have an article dedicated to everything you need to know about fungus gnats and even how to get rid of them.
Hope this helps, Organoman.