Casing, moulding, and trim can hide many sins, but as you have found out it cannot easily hide large problems with the underlying rough carpentry. This is especially so with problems related to nailing edges.
Installing trim is a bit of an art, especially since much of it is installed by someone other than the person who built the framing, or hung the door or window.
- Make sure there are decent matching nailing edges, especially at the corners. A level or straightedge can be used to check for this, and then any necessary shims are installed prior, or in combination with, the trim.
- Good trim requires a good amount of dry-fitting. Most trim can be tested for dip and sag with modest pressure (unless you live in an older home where sometimes it feels like they installed load-bearing trim). Either shim or provide a more stable nailing surface if things aren't lining up.
Trim is always an approximation of "true". It is an imperfect world, and trim is there to hide imperfections and provide easier to repair wearing surfaces. Getting mitres to meet nicely on 3 axes is probably why many modest houses have butt-joined trim!