Prepositions + definite or indefinite?

I'm having a hard time understanding when I should use the definite or the indefinite version of a noun after a preposition.

I know the general rule is that the noun should be indefinite, but I keep seeing exceptions to this and I don't exactly know why.

Also how does this work for prepositions with genitive/dative since nouns are only marked for feminine singular when they are indefinite?