I would guess that they are either detritus worms or planaria, more likely the former. They are harmless but ugly. The only way I know to get rid of them are regular water changes/substrate cleaning. If you remove the substrate it will probably kill your beneficial bacteria so I wouldn't do that unless it is out of control.
I've got detritus worms at the moment and am removing them with my turkey baster every time I see them. Every week or so I take my axie out and rework my sand substrate so any worms come out and I can remove them with a water change. The water will take a few hours to settle after, but it seems to be working so far.