What causes the DNA fragments to stop moving in gel electrophoresis?
There are electrophoretic methods where the molecules stop automatically like Isoelectric Focusing, but in the common agarose gel electrophoresis of DNA you mention this is not the case.

The DNA is negatively charged, it will move along the electric field you set up until you stop applying any current. It will happily move out of your gel if you let it run for too long.

To see the progress of your gel some dyes are usually added to the sample like bromophenol blue or xylene cyanol. They don't interact with the DNA, they just provide you a reference on how far your gel has run. You still need to have a rough idea how your samples run compared to those dyes if you want to use the full length of your gel.