I always paint the trim first (unless I need to prime...then I prime first...trim second). Lay down painters tape to protect the carpet. Then paint, paint, paint.
Since I am usually painting over a weekend, if I peel the tape up when I am all done, I risk pulling paint off of the trim as well.
So grab a utility knife and gently run the blade along the edge of the trim. Remember, you don't want to cut your carpet, just cut through the dried paint.
Then peel the tape up!