A syntax error occurs when a programming language's rules are violated, preventing the code from being compiled or interpreted correctly. These errors are typically caught by the compiler or interpreter and must be resolved for the program to run successfully.