What is the role of pH in azo coupling reaction of diazonium with phenol and aniline?
Deprotonation of the phenol and protonation of aniline result in species that easily react with a diazonium cation in the intended manner.

Let's have a look at the species involved. On the one hand, there is the diazonium cation:



Deprotonation of phenol yields phenolate, for which a resonance structure with a negative charge in para position to the substituent can be written:



We conclude: Deprotonation of the phenol means activation!



In the case of aniline, the lone pair on the nitrogen atom is the preferred centre for the reaction with the diazonium cation:



This isn't what we want! Protonation of aniline avoids this side reaction.

We conclude: Protonation of aniline means "masking" the wrong centre!