Middle English sweren, from Old English swerian, swerigean, "take or utter an oath, make a solemn declaration with an appeal to divinity" (class VI strong verb; past tense swor, past participle sworen), from Proto-Germanic *swērjanan (source also of Old Saxon swerian, Old Frisian swera, Old Norse sverja, Danish sverge, Middle Dutch swaren, Old High German swerien, German schwören, Gothic swaren "to swear").
This is of uncertain origin. The old explanation (Pokorny, Watkins) has it from a PIE *swer- "to speak, talk, say" (source also of Old Church Slavonic svara "quarrel," Oscan sverrunei "to the speaker"). Boutkan suspects a substratum word, or, if it is IE, writes that a connection to Latin verbum "seems more promising."
It is related to the second element in answer. A Middle English noun sware meant "an answer, a reply; speech, utterance," from Old English -swaru, and from the Old Norse cognate.
The secondary sense of "use profane language" (early 15c.) probably developed from the notion of "invoke sacred names profanely or blasphemously" (mid-14c.).
[Swearing and cursing] are entirely different things : the first is invoking the witness of a Spirit to an assertion you wish to make ; the second is invoking the assistance of a Spirit, in a mischief you wish to inflict. When ill-educated and ill-tempered people clamorously confuse the two invocations, they are not, in reality, either cursing or swearing ; but merely vomiting empty words indecently. True swearing and cursing must always be distinct and solemn .... [Ruskin, "Fors Clavigera"]
To swear off "desist, abjure, renounce solemnly, as with a vow" is by 1839. To swear in "install (someone) in office by administration of an oath" is attested from 1700 in modern use, echoing Middle English, where to be sworn was to be admitted to office by formal oath (c. 1200).
To swear by is from early 13c., originally in reference to a divine being or sacred object; the colloquial sense of "treat as an infallible authority, place great confidence in" is by 1815.