Thankfully those are called detritus worms and are completely harmless. They normally come from any new fish, plants, and sometimes decorations that are purchased that are already within water.
They can be eaten by fish, shrimp, and sometimes snails, although they are mostly found to be hiding within the substrate or areas that have high debris (in which they break down as they eat the extra nutrients within these as their food source).
If you have only a shrimp tank, or too many debris that aren’t being cleaned up, they can and will over time start to appear on things within the tank vs. staying hidden in areas not commonly looked at. If you want, you can simply suck them up using a gravel vacuum and cleaning the substrate.