Summary:
Spies have a small chance of getting caught whenever they steal technology. If the target city does not contain a counterspy, the possibile outcomes are Undetected, Detected but Unknown, and Identified. If there is a counterspy present, the possible outcomes are Detected but Unknown, Identified, and Identified and Killed. Having your spy either Identified, or Identified and Killed will negatively affect your relationship with the target civilization. If your spy is killed, you will receive a replacement, but not for a few turns.