Stack overflow occurs when a program uses more stack memory than is allocated, often due to deep or infinite recursion or excessive memory allocation. It typically results in a program crash or unexpected behavior, necessitating careful memory management and recursion control in programming.