I don’t really know the full answer to this, because I’m not a chemist, but I’ll give it a try! I guess that there are certain types of chemical compounds that react reliably differently when they’re mixed with an acid than when they’re mixed with a base. These compounds are used as pH indicators, because the different compounds that they create absorb light in different ways. As a result of that, it makes them good visual indicators of whether something is an acid or a base, because they will change colour depending on what they’re mixed with. I’m afraid I don’t know how it works in any more detail than that though, sorry!