1. Decide how you want the trim to cover the gap.
2. Set the compass to the widest part of the gap. Rest its metal point on the wall and the pencil point on the spot where you want the edge of the trim to land on the cabinet. Your trim must be at least this wide.
3. Plumb the trim piece. Hold the trim against the wall with a level on one edge, and adjust the trim until it's plumb and still touching the wall. Tack or clamp the trim to the cabinet.
4. Mark the edge to be cut.Without changing the compass setting, rest the compass' metal point on the edge where the level was. Keep that point and the pencil point level relative to each other and make a mark on the trim in the vicinity of the widest part of the gap.
5. Scribe the edge to be cut. Reset the compass to the widest distance between the mark and the wall. Without changing that setting, hold the metal point against the wall and the pencil point on the trim and run the compass the entire length of the trim to make the scribe line. Be sure to keep both points level the entire time.
Now take the trim piece down and cut it with a jigsaw along the waste side of scribe line. That way, you'll have some material to sand or plane down for a perfect fit. — Mark Powers