What are the factors behind crew pairing and crew rostering?
It is very, very complicated.
It is so complicated that an entire department is dedicated to this task. Below I would provide a glimpse of crew rostering.
Basics
Most airlines provide roster on a monthly basis. The flights that will be flown are well planned beforehand. From this, a minimum number of crew must be on each flight, for example a Captain, a First Officer, and certain number of flight attendants. Many requirements and restrictions must be meet. For example:

Crew must be qualified to perform their duties (more on that below).
Duty time, rest time, ground time, turn-around time must be legal, and preferably, reasonable.
Mandatory crew may be different for different aircraft types. For example, a B777 on a long haul flight may require 2 Captains, 1 First Officer, 1 Second Officer and 10 flight attendants. A A320 on a short flight would require 1 Captain, 1 Second Officer, and 4 attendants.
Generally speaking, cabin crew can be swapped between aircraft types, such as between Airbus and Boeing. Pilots are current (legally qualified to fly commercially) only on a specific type.
Crew are sometimes assigned to ad-hoc duties, such as accepting delivery of a new aircraft.
Besides flying, crew would also have training duties, line-check duties and simulator duties.

Constraints
Mostly for safety reasons (and also for risk reduction), some pairing combinations are not allowed either legally or by company requirement. For example:

A pilot aged 60 or above must be paired with another pilot below the age of 60.
A newly joined Captain (defined as having flown X sectors or less) must not be paired with a newly joined First Officer or Second Officer.
Similarly, the percentage of newly joined cabin crew (defined as having flown X sectors or less) should not exceed a certain number.
Some flights may call for an experienced crew, for example a new route (no one has landed at that airport before) or charter flight.

Qualifications
There are various qualification requirements that must be fulfilled for a crew to legally perform duty on commercial flight. For example,

Captain must have made a takeoff and landing within the previous 30 days.
First Officer or Second Officer must have made 3 takeoffs and landings within the previous 90 days.
Certain qualifications (e.g. takeoff and landing requirements above) expire, but can be renewed on a commercial flight. You'd want to plan it so that the pilot has a chance to attempt it during duty hours, instead of going through additional training after it has expired.
Certain qualifications must be renewed through training, for example in a simulator. You'd also want to plan it so it is renewed before flying status is invalidated. Remember that the trainer may also have flying duties as well.
Route may require additional ratings. For example crew must be ILS CAT-III trained to use it. If it flies over Chinese airspace crew must be trained to fly metric system. If it flies over the Atlantic crew must be trained to fly North Atlantic Tracks.
Line-check needs to be performed every X number of days.
Crew must possess the necessary nationality or Visa for the departure / destination airport.

Ad-hoc / stand-by
Unforeseen circumstances such as mechanical problems, adverse weather, on-route diversions, crew sickness etc. may require last minute changes to the roster list. This is undesirable, so a certain number of crew would be on &quot;stand-by&quot; duty, meaning there is no duty assignment on that particular day, but they must be available at a particular airport within X minutes shall the need arises. There are legal requirements on stand-by time and duty time after stand-by as well.
If the department does not plan well, there may be insufficient number of crew members (i.e. not enough manpower) to serve all flights.
Improvements
Fatigue is becoming more of a hot topic in aviation. Just because a duty assignment is legal and within company policy, does not necessary mean it is good. Software can be used to analyze factors such as timezone shifts, jet lags, day night patterns etc. to identify and avoid possible fatigue scenarios.
On certain routes, it may be beneficial to assign crew who can speak certain foreign languages. For example, if statistics show that a portion of the passengers on a route are Koreans, it would be better to assign it to cabin crew who can also speak Korean. It may even be a company requirement that the cabin crew together must speak a few chosen languages, so at least there is someone who can translate.
How it is done
As you can imagine, given this many constraints, the roster is done by software. It is mainly a constraint satisfying problem, which can be solved by many well-established algorithms in computer science.
Some rules are &quot;hard rules&quot;, meaning they are legal requirements. Some are &quot;soft rules&quot;, they are best accommodated to provide optimal performance but may be violated when necessary. For example it is preferable to fairly allocate flight and duty time. It is also preferable to fulfill any route preference requested, but that may not be possible given other constraints.