This answer by /u/BBlasdel addresses the question pretty well.
Edit: at least, it answers the question fairly well for Christianity. There’s some discussion of Judaism in the other comments on the same post, but if you’re looking for answers about Islam, you might have better luck searching for that specifically.
Follow-up/specification of OP's question, if I may.
Many cultures place great value on virginity at marriage, especially for women. Unmarried women who aren't virgins are treated cruelly as "damaged goods"; grooms and their families obsess over the bride's virginity to the point of looking for (known-to-be-inaccurate) signs like intact hymens and blood during the wedding night.
The armchair evopsych rationale for wanting to control women's chastity within marriage is obvious enough: the husband wants to be sure that the children he ends up legally and economically responsible for, are really his. But I don't see how and why this should extend to women having sex before marriage. Unless the bride shows up pregnant or with a child on her arm, there is no real "risk" to the groom of raising another man's children – except if the premarital sex happened so shortly before the wedding that she may be not-yet-visibly pregnant. Seems like cultures could check off that box with more time-specific rituals – prove menstruation the last three months before the wedding, something like that – rather than condemning a bride for any and all sex, no matter how long ago.
Where does this obsession with bridal virginity come from? And why is it so expressly placed on women, when men have a far greater risk of unknowingly siring random bastards (who may come back to bite them) if they sleep around before marriage?
This concept is bound up in the Jewish and Christian understanding of "sexual immorality". What this meant is a difficult question to answer because words change their meaning over time, and the two words for sexual sin (porneia "fornication", and moicheia "adultery") have particularly changed their meaning.
In Classical Greece, the word moichea meant only the "violation of a respectable woman". A respectable woman was specifically "one whose sexual activity was of concern to a citizen male". They were called eleutheria, and were wives, daughters, widows, anyone whose sexual activity was regulated and controlled by a citizen male. This obviously left out any woman who was not eleutheria, such as a prostitute, slave, foreigner, outcast, etc.
A eleutheria did not have to be married for moicheia to occur, a man who had sex with an unmarried daughter without her father's consent (or more accurately, the consent of her kurios or "lord", who could be her father or another citizen male) would have committed moicheia just as much as if he'd violated a married woman. So in that sense, the English word "adultery" is too narrow a term to signify it. Obviously, the woman's consent was unimportant to the Greek's concept of moichea, the violation was understood to have been committed against the man who controlled the woman, not the woman herself. And the moichos was understood to have violated another's honour, not his own, there was no sense of having broken his own marriage bond. Similarly, there was no female equivalent, an eleutheria could not commit moichea herself, as a woman had no personal honour to violate.
In classical Greek, the word porneia referred specifically to prostitution, and specifically to the practice of selling one's own body, not the institution of prostitution overall. Despite the fact that pornai were ubiquitous in ancient Greece, as courtesans and prostitutes were considered essential, the word is extraordinarily rare in Greek writing. It is also interesting that only the seller was committing pornos, the buyer was not. There was no word in Classical Greek for the person who bought sex from a porne. Perhaps this indicates the practice was so common as to not need a word.
However the word took on greater significance when it was adopted by Jewish writers. This was due to the greater range of meaning in the underlying Hebrew word zanah. Unlike the Greek porneia, this word was used to describe the agency and moral failing of women. However, although it often translated in English as "harlot" (KJV) or "whore" (NRSV), this is a mistake. The word means "to fall into sexual shame", and is a general term meaning female unchastity and sexual dishonour. It’s true that a prostitute would be understood as a zanah but this was because she was habitually unchaste, not because she was selling herself. A wife or daughter would also be equally a zanah, even if she only had a single love affair. Rather than being translated as "whore", the word means something more akin to the English "slut". The verb form is often translated as “to play the harlot” or “to prostitute oneself”, but its meaning was more, “to be unchaste”, or the more visceral, “to be sluttish”.
Over time, the Jewish prophets began to expand its meaning, and used the term as a spiritual metaphor for Israel's sin against God in its idolatry. Hosea first began to describe unfaithfulness towards God as spiritual zanah. This metaphorical meaning allowed the word to begin to be used with acts of male commission, rather than just with female.
Therefore, during the second temple period, the term zanah began to be used to describe both male and female illicit sexual activity. This was a radical change in the use of the word.
By the time we reach the book of Sirach in the second century BCE, we see evidence that the Greek word porneia had shifted its meaning also, and was being used in the more expansive sense that zanah had taken on. Porneia now also included “a broadly conceived range of sexual vice”, among which Sirach included the radically expansive “looking at a courtesan”, “gazing at another man’s wife”, and “meddling with his servant-girl”.
In the Testaments of the Twelve Patriarchs (2nd century CE but likely based on an earlier work of Hellenistic Judaism) we see it used as a “catchall vice for any sexual transgression”, including the most petty of voyeuristic sin. In it Issachar claims that “Except for my wife I have never known another woman. I have not committed porneia by the uplifting of my eyes”. For the Testaments, porneia had become the principle vice and “mother of all evils” (T. Sim. 5:3).
This is seen in other Jewish writings of the time. In the Book of Tobit, it is included as a referent for marrying a foreign woman outside the tribe. The Damascus Document describes porneia as remarriage after divorce. For Philo’s ‘The Life of Moses’, he describes the legendary heresy of Peor as a scene of general sexual licentiousness. At first he presents this sexual license, including prostitution, as a means by which Balaam seduces the Israelites into sacrificing to false gods. But by the end, the king has abolished all sexual laws entirely and “ordered the women to have intercourse freely with any of the men they wished”. There is no commercial transaction, only general sexual license, and it is this which is seen by Philo as the ultimate sexual sinfulness.
Therefore, by the time of the New Testament writings, the term had lost its distinction as a reference to prostitution specifically, and had come to refer to any and all sexual activity the writer considered illicit. In this sense, both porneia and moichea could be largely interchangeable terms, as moichea was a form of porneia. Moicheia however remains a violation of a male’s rights over a woman, and does not imply female agency or moral failing. That is presumably why Matthew uses the word porneia in 5:32 and 19:9. (“Everyone who divorces his wife except on the grounds of porneia…”) rather than the more expected moicheia, in order to signify the wife’s own moral failing in the matter.
However, while porneia was a general term for sexual licentiousness it is important to note that the major cultural distinction between Jews and pagans in regards to sexual license was prostitution. For many Jews this would have been the most visible “hot button issue” of their day. For Philo, writing in the first century, he places the following words into the mouth of Joseph as he resists Potiphar’s wife: “We descendants of the Hebrews live according to a special set of customs and norms. Among other people it is permitted for young men after the fourteenth birthday to use without shame whores, brothel-girls and other women who make a profit with their body. Among us it is not even permitted for a professional woman to live…” (De Iosepho 40-42). Of course Philo was representing how the Jews of his own day saw the distinction between Roman culture and their own. Prostitution was the most clear and important distinction between Jewish and pagan understanding of porneia, and so it is likely to have been foremost in the minds of the Jewish writers and readers of the time when they heard polemics against it.
Perhaps this helps us to understand the otherwise enigmatic use of the word porneia in the apostolic decree in Acts 15:20, 29, 21:25. It is the only prohibition which isn’t a dietary requirement, and it is an extremely unspecific word. Does it refer to prostitution specifically, to the specific prohibited unions of Leviticus 18, or to a more general sense of the word to cover any sexual license whatsoever, even marrying outside one’s tribe or “the uplifiting of the eyes”? The LXX does not call the Levitical prohibited unions porneia yet would that have been the understanding of Paul by the 1st century?
Interestingly, in 1 Corinthians 5:9, Paul warns the Corinthians not to associate with sexually immoral men, referring to those men sleeping with their father’s wife he has just mentioned. For Paul, therefore pornois in this context refers to male sexual sinners in general. Yet in 1 Corinthians 6:12, he defines it specifically as men who have intercourse with porne “prostitutes”, which he firmly prohibits.
For Paul therefore it appears that although he can and does use *porneia” as a general descriptor of illicit sexual activity, the most prominent illicit sexual activity he has in mind is prostitution, an easily available and entirely socially-acceptable activity in Greek and Roman society, but one which Paul and other Christians abhorred. This was the reason for Pauls’ promotion of marriage “because of porneias “acts of sexual immorality”. Although pagans saw porneia as the socially-accepted solution to the temptation of moicheia, Paul sees marriage as the necessary solution to the temptation of porneia.
See Part 2 below