Fragmentation refers to the inefficient use of memory space. Internal fragmentation is the wasted space within each allocated memory block due to rounding up to the next allocation size. External fragmentation occurs when various unused memory holes are scattered throughout the memory space. The sample code demonstrates how to calculate internal and external fragmentation by allocating memory to processes and tracking the remaining free space.