Out of all the bits of royal protocol - and there are tons - one of the most visible ones is how everyone walks when the queen is present. Specifically, you might have noticed that no one ever walks in front of the queen in public. As it turns out, that's more than politeness. It's actually a cardinal rule of royal etiquette, and it all comes down to one very simple explanation: hierarchy.
In the royal order of precedence, no one outranks the queen, so no one walks in front of her, period. This even includes her husband, Prince Philip, who has, since Elizabeth became queen, walked a couple of paces behind her at all official functions, rather than side-by-side. The order of precedence is pretty much the same as the line of succession to the throne: the closer you are to the throne, the higher you are in precedence. For instance, the queen is always first, of course, followed by Prince Charles and the Duchess of Cornwall, then the Cambridges, and so on. Additionally, when it comes to married couples, the blood royal always outranks their spouse - so, for instance, there may be times when Kate has to walk behind William, or Camilla behind Charles. It's a lot like the rules about bowing and curtsying: higher rank means you get bowed to, and it also means you get to walk first!
It's a little bit less cut-and-dry when it comes to people who are peers to the queen, such as heads of state from other countries who are not affiliated in any way with the British Commonwealth. In many of those cases, it's polite to let the queen go first, but not as strictly required. For instance, an American president would, typically, offer the queen the courtesy of letting her lead the way, but it's not bound by the same orders of precedence. It's more about the etiquette. And if there are other monarchs involved? That's a whole other complicated web of etiquette that we mere commoners aren't always privy to, but presumably, since other monarchs would be of equal rank, factors other than pure rank would come into play.