Virtual memory allows a program's logical address space to be larger than physical memory by swapping pages in and out of memory as needed. Demand paging brings pages into memory only when they are referenced. When a page is not in memory and is referenced, a page fault occurs which causes the OS to locate a free frame, swap in the page, and update tables. Page replacement algorithms like FIFO and LRU are used to determine which page to swap out when there is no free frame. Thrashing can occur if working set size is larger than physical memory.