In modern windows, programs tell windows where they'd like to be placed in memory. If that can't happen, then windows goes through and changes the instructions so that the program will run properly at a different address.
As for DOS, I think it used a system similar to TI-OS to load programs, but I'm not sure.