Short answer:
The captain has to divert to the nearest airport in case the urgent medical attention will save a life.
Long answer:
The first thing the crew should do once there is a sick passenger is looking for a medical professional onboard. A doctor will be preferred but a nurse or so will do the job in case no doctor is available. Now if there is a medical professional onboard then he/she will be in charge of deciding whether to land the aircraft in the nearest airport or not (emergency landing).
The confusion comes when no doctor is available onboard, usually the pilot will decide to land right away in case of heart attacks or seizures or when the passenger is unconscious. Meanwhile the crew will try their best to save the passenger's life, all crew members are trained for Basic Life Support and almost all aircrafts are equipped with AED, doctor kits and first aid kits.