Defragmenting does not change how things work. It just improves access speeds for some files.
Imagine a file which occupies sectors 1000, 1001, 89975438, 899754389, 902 and 903.
Moving the data around so that the sectors are all in a row means that the heads don't have to zoom forwards and backwards, which is the slowest operation on a disk drive.
Windows 7 defragments drives on a timer, usually set to the middle of the night; Windows 8 and 10 do it automatically when the machine is idle.
If you have a solid-state drive, defragmenting doesn't improve speeds; it takes the same amount of time to reach any sector. Indeed, it wears out the drive as flash memory can survive only a limited (if large) number of cycles. Windows 8.1 and 10 are usually able to spot solid-state drives and disable automatic defrag.