The trim that surrounds a door or window frame is called casing. Since they have to butt against the casing, it is always installed before the baseboards and chair rails. Baseboard and some other molding must fit against door and window frames before casings are installed in a room.
Trim must be painted correctly as it is frequently contacted by humans, pets, and furniture being moved. Ori Avisar has a wealth of experience choosing the right paint and painting technique for casings.