I can tell you exactly what it is. And it is NOT planaria. It is a harmless worm called detritus. You can do some research on it. I started getting them bad when my glofish started breeding and the eggs and fry that don't make it out the rocks die and these worms thrive off of the waste. The reason for them coming out of the gravel is because of an explosion of the population and low oxygen levels. Also I feel like live plants bring them in.
They only way I found to make them less visible is upgrading the filter. This provides better filtration and more oxygen. The cleaner it is the less worms there will be. The fish eat them though and it is a good source of food. Especially for fry who need to eat constantly.
Can you tell me your water parameters and the siZe tank and which filter you are using?