In order to understand how pH affects the structure and function of enzymes, one must first understand what enzymes themselves are. Enzymes are biological molecules that act as catalysts to speed up chemical reactions. These biological molecules are proteins made up of the 20 various amino acids that exist in nature. All amino acids share the same basic structure, having an amino group (NH3), carboxyl group (COOH), and an R-group also known as a side chain situated around a central carbon bound to a hydrogen. These side chains vary from amino acid to amino acid and are what make each one unique. Amino acids can therefore also be classified into different subgroups based on the chemical properties of their respective side chains. These side chains are important in determining the overall final structure and folding of the enzyme which determine its function.
Looking at your question more specifically, different pH levels can affect enzymes by interacting with their amino acid side chain groups, causing alterations to the structure of the enzyme. This is due to the ionization of side chains from either an acidic or basic solution, and depends on what side chains are present. Because each enzyme consists of different amino acids, different levels of pH are best for each enzyme to function at its optimal level. Changing the ionic bonds of an enzyme's side chains by placing them in solutions of different pHs leads to altering their 3-dimensional structure. This change in structure changes the conformational shape of the enzyme where interactions are occurring (i.e. the area of your image where food molecules are binding). Changes from the optimal structural conformation causes molecules being catalyzed in the reaction to fit improperly into their binding site. This decreases the enzyme's ability to function properly, leading to less enzymatic activity, and a decrease in overall function. Hope this helps!