‘Come’ is both a noun and a verb, referring variously to orgasm, to the semen produced by the male orgasm, or even to vaginal fluids produced by sexual intercourse.
Cum is merely an alternative spelling of ‘come’. It too can be used as noun or verb. The misspelling is intended to look crude. This emphasizes the sexual or vulgar meaning.
The noun meaning (semen) was the origin of the ‘cum’ spelling and is still its primary usage.
The sexual meaning of ‘come’ began with the verb form (to orgasm) as early as 1600. The earliest usages of ‘come’ refer to both his and her orgasms, but it appears that the male ejaculate was the origin of the usage.
It’s probably derived from the phrase “come forth with,” meaning to bring something out either figuratively or literally.
A similar but non-sexual phrase in the 1600s was ‘[to] come it’, meaning to accomplish something. Another similar meaning was the 1800s criminal slang phrase, ‘Come it’, meaning ‘tell it’ or ‘confess’.
‘Come’ is such a common word with many different meanings. That explains its popularity with writers. The ambiguity of ‘come’ is part of the pleasure.