DNA fragments are negatively charged. When an electric current is applied to the gel, the negatively charged DNA fragments migrate towards the positively charged electrode.
The shorter DNA fragments migrate through the gel more quickly because of their lower density while the longer DNA fragments move through the gel more slowly. This results in the fragments being arranged in order of size.