First... Fragmenting occurs due to writing and rewriting files back and forth to the hard drive as the compute uses and saves information. During this action files don't always get put back exactly where they came from, but that is handled by the hard drives index, the File Allocation Table or FAT.
What Defragmenting does is rewrite all the related files back into organized blocks on the hard drive. By doing so the computer doesn't have to look as hard for the file by having to run through a long index.
Another analogy would be taking all the file folders that are just randomly stored in a file cabinet and reorganizing them in alphabetical order, grouped in specific drawers.