Syntax highlighting works by parsing the code and applying different colors to various elements such as tags, attributes, and values. For instance, in HTML, tags might appear in blue, while attributes are in red, and their values in green. This visual differentiation helps in quickly identifying elements and understanding their relationships.