Bowing seems to be a universal human gesture, as by making yourself lower than the person you are bowing to and making yourself vulnerable to attack by lowering your head and not looking at them you show respect in an unmistakable way. Similar gestures exist in other animals. However, in most European societies bowing has almost died out, remaining only for kings and queens and possibly from servants to masters, and I think these vestiges give a clue to why it still exists in Japan more generally.
Politeness in Japan is fundamentally different from politeness in modern Britain, to take an example of another country that is famous for its manners. For example, in a shop in the UK the shopkeeper and customer will say please and thank you an approximately equal number of times, and the body language and tone of voice will also convey the illusion that both sides are equal. In Japan, the customer is king, and the king will often show that with a lack of the bowing, polite language, avoiding eye contact etc that the server will use, and in a convenience store will often not say a word during the whole interaction. The language and body language of interactions with bosses, sempai etc. often work the same way. Therefore, politeness in Japan is still a way of showing distinctions in status between people, whereas most politeness in the UK is now to pretend that those differences don’t exist.
More on bowing (not including my theory!) on the Wikipedia page here, including the interesting theory that the “scraping” in “bowing and scraping” comes from the foot moving backwards in a Elizabethan bow.