The first thing you need to understand is the difference between a physical address and a virtual address. When your program writes data to a particular (virtual)memory address, the address of your write gets translated into the actual address on the RAM chip. If a physical address is not programmed in for a particular virtual address, in you will normally get an access violation or segmentation fault.
The way that this works is the Memory Management Unit (MMU) generates an CPU exception which causes execution to jump to a kernel fault handler. In the access violation case, your process is terminated.
The kernel is able to do some other clever stuff. If your process is idle, it might copy some of the physical memory used by your process into a swap file (and remove the entries from the mmu), when you access the said addresses, the CPU exception will not abort your program. Instead the kernel will first suspend your process. It then will copy the data from the swap file back into memory, reprogram the MMU. The kernel will then resume your process and the process will access the memory as if it had always been there. As far as the process was concerned nothing special happened. This process is known as Demand Paging.
When you mmap
a file, the kernel is effectively performing demand paging but from your read specified file and not from the swap file. The process is exactly the same. The CPU access exception will cause data from the file to be read into RAM which you access as normal memory. When you close the file, the memory gets written back into the file deallocated. The big advantage is that your get to use the data in the file with pointers.
Hope this helps