Bit postal-nerdy, sorry
In general letters go in to the machines with known orientation - bulk mailings (e.g. your bank statements) are delivered in trays with consistent facing, and for collection (i.e. out from a postbox) letters will be pre-processed through a machine called a culler-facer-canceller (CFC) which removes items that are too small, too large, or too thick (“culling”), orients so that the ‘indicia’ (stamp or pre-paid licence) is outwards (“facing”) and cancels the stamp. That’s not perfect for facing, but usually pretty good. They then go into the main sortation machines, one type has two cameras so we get both sides and can pick if the CFC got it wrong, the other type is single camera and misfaced mail goes to manual refeed, so a person can flip it over and feed it back through. There’s a bit more to it than that, but hopefully gives you the general idea.
Facing uses a mixture of ABL and indicia detection to try and figure out the address side, bulk print also also sometimes has things called FIMs (Facing Indicator Marks) that make it simple for the CFC, but they’re less common these days.
Increasingly letters use barcodes too - known by Royal Mail as MailMark, there are three types of datamatrix barcode that encode information about delivery point, along with a few other things. We use that information if it is available on a letter - barcodes are pretty easy to detect - but will use the written address in preference to the barcode for the final result if the two differ.
Not a bug as such, as if the item isn’t addressed to spec then sortation is best effort - you’d be amazed at how strangely some items are addressed and an automated system will never catch all the edge cases. Still, the system usually avoids sorting to sender, typically it would go to a reject stacker for human intervention if the system’s not sure, so you got unlucky.