How to fill space between wall and door
If it is less than an inch, caulk will fill it fine before you cover it with trim.

Use a caulk that has high flex characteristics like a polyurethane based caulk, actually latex with silicone would work too.

Don't need to be too careful, trim will cover up the caulk.