I am going to assume that what you are seeing are fungus gnats. If so, they live for only about a week before dying of "old age." That's the good news. The bad news is that more are on their way from the gnat larvae that live in the upper portion of the soil. So the key is to treat the larvae, not the winged adults that will die soon enough on their own.
Gnat larvae are usually introduced when plants are repotted using contaminated potting soil. The best treatment of the larvae is to allow the top inch or so of the soil to dry before watering. The larvae require constant moisture to survive. Unfortunately, Maidenhair ferns do not tolerate dryness so you may end up killing the plant as well as the gnats. You could try to scrape off and remove any new soil you may have added to the top of the original rootball. That may eliminate a lot of the larvae.
Otherwise, you can try a product such as Gnatrol that is reasonably effective. You might also have success sprinkling a very light, thin layer of sharp sand over the surface. The sharp sand grains will carve up the larvae as they try to emerge from the soil.
Good luck!