It's actually not always clear. There are "Einkommensteuer" und "Einkommenssteuer" and you could write and pronounce them either way. Same for "Bahnhofstraße" and "Bahnhofsstraße". But yeah, I guess these are some rules that developed around the practice of writing words together.
It's also interesting when you split up the words. For instance "gebrandmarkt" results in "brand gemarkt". So you extract the "brand" from "ge-...markt".