How to make a re-programmable microcontroller
You want something called a bootloader.  This is code that runs in the micro that can accept new code from externally, like a USB port, and write that to the micro's program memory.  Then there also needs to be code that looks at the current program image, the new uploaded one, and decides whether to copy the latter onto the former.

There are a lot more details about bootloaders at What is a boot loader, and how would I develop one?.