Why does pH have an effect on enzymes?
Proteins are polymers of amino acids. Each amino acid has a side chain. Many of these side chains contain ionisable groups. The ionization state of these groups is dependent on the pH. A group that is protonated at pH=2 and neutral, for example, may become deprotonated at pH=8 and become negative. See the Henderson-Hasselbalch equation.

Protein structure is held together by many intermolecular interactions: dipole-dipole, hydrophobic, electrostatic, etc. The correct protein structure is necessary for function. At different pH levels, the protonation and deprotonation of ionisable amino acids affects the intermolecular interactions within the protein and its conformation will change. At a certain pH, the conformation of the protein will be optimal for its function and this is where it will have maximum activity. As pH deviates to either side of the optimum, the conformation changes and the structure will no longer be correct for proper function.

The ionization state of amino acids is also important in the active site but not only because of conformational changes. Catalytic residues must have certain properties to catalyze their specific reactions. As an example, two Asp residues (which have carboxylates on their side chains) in the active site of DNA polymerase are responsible for coordinating two magnesium ions that promote polymerization. If the pH were to decrease to a point where the Asp residues were protonated, they could no longer bind the Mg2+ ions and the enzyme would not function.