If the drywall's recessed behind the jamb, don't nail the trim to the framing at first. After the window is trimmed, slide shims behind each nail location to hold out the trim while nailing, then cut off the shims. Caulk the perimeter of the window casing trim to eliminate gaps before painting.
How do you hang trim?
https://www.youtube.com/watch?v=aqnQ5Jkozgg
How do you install a sill trim?
https://www.youtube.com/watch?v=iBfUbQSmMrE
How do you secure a window sill?
https://www.youtube.com/watch?v=-dVNP69ecW8
How do you nail window trim?
https://www.youtube.com/watch?v=86d5N65p668
What nails should I use for window trim?
Use a brad nailer for this part of trimming a window. Attach the trim to the jamb with 1 1/2-inch 18-gauge brad nails in 6-inch increments. Use 2-inch or 2 1/2-inch brad nails for the thicker part of the trim boards.