Our amazing breathing sacs, evolved from an invagination of our gut. Had that invagination been dorsal rather than ventral, we might be spared that awkward crossing of tubes that makes eating and breathing at the same time so dangerous task.
Id did not go too much into detail here, but I did outline the branching of bronchi and bronchial tubes somewhat. Which is an interesting issue in and of itself.
The tubes do not simply split willy-nilly, the branching is really intricate and is optimised so that at every fork the air pressure stays the same, thus guaranteeing that all tubes contribute to the air exchange and there are no (ideally) pockets of stationary air. This is no easy task for engineering, piping is difficult to make and ventilation systems in buildings often need additional ventilators added to the pipes at various places to compensate for pressure drop at some junctions.
In our lungs this is partly achieved by the fact that there are no right angles anywhere and partly by the fact that the forks are arranged in such a way as to reach all the alveoli with the shortest possible amount of piping. In modern engineering such tasks beyond certain level are allegedly sbest solved by evolutionary algorithms, because there are too many variables involved for simply designing them right of the bat.