It is permissible, though detested, for a man to withdraw (not to ejaculate) his semen in his wife during intercourse. This is the verdict (of the school of thought) of Imam Abu Hanifa, Imam Malik and Imam Ahmad. The three Imams agreed that it is a condition the wife consents. The followers of the Imam Shafi school of thought differed: some of them agreed with the majority in this condition, some of them allowed it without any condition.
Imam Nawawi explains that: “withdrawal is the ejaculation of semen outside the woman’s vagina during intercourse; and we detested this act in all ramification (makrooh) whether the wife consents or not.” He went on until he says: “as for the Noble woman if she consents to it, it is not forbidden, but if she objects, there are two views and the most correct view is that it is not forbidden.”
“With this, analogy is made to every other contraceptive methods the spouses or any of them may use, such as pills used by women to prevent pregnancy whenever they have sexual intercourse. Whatever method they agreed on must not harm them Health wise and should be based on the advice of a trusted doctor.” (Commentary Sahih Muslim V.10/9)
The above explanation and ruling are for legally married couples. As for the case of formication, the fundamental ruling is that it is forbidden.