It is difficult to definitively say how it first came about, but there are some key factors that can be used to determine.
First, the main difference between sexual and asexual reproduction is that sexual reproduction “reorganizes” the parent DNA into a new pattern, while asexual is essentially the parent self-cloning. By being able to reorganize DNA, sexual reproduction had an advantage over asexual reproduction because it caused more mutations to occur in the DNA. This leads to a higher chance that a mutation will be beneficial to the organism, especially because the baby organism gets two sets of DNA to choose from, doubling its genetic resources. It also means that if one parent has a successful mutation that the other parent does not have, the baby organism will have a better chance at acquiring that mutation.
Second, we know that with two (or more) sexes, one sex must be more involved in the reproduction than the other. In most species, this is the female organism, which is able to do the reproductive work of bearing young. The male organism can merely contribute DNA, but does not actually bear young. With 50% of the population unable to make the babies, conditions must be good enough to allow the other 50% to reproduce often enough and quickly enough to maintain the whole species’ population. This means temperature, food sources, and safety from predators or environmental threats needed to be ideal.
We can break down the evolution of sexes a bit more; there is a term “anisogamy” which means reproduction with sperm and egg. This first evolved in tiny cells that contained only one set of chromosomes. Chromosomes are chains of DNA. When the cells would reproduce, the egg would provide one chromosome, and the sperm would provide a different chromosome. For a brief period, the baby cell would contain both chromosomes, and after a short period of development one of the chromosomes would “win” and that chromosome would become the DNA pattern that the new cell would eventually pass onto its own young.
This “anisogamy” developed when two compatible cells evolved, that were slightly different but able to mate with one another. In the first tiny cells that accomplished this, they were too similar to really be called “male” or “female,” but the difference was enough to spark the eventual evolution of male and female.
So, to recap:
Single cells would split in half to reproduce. Then, cells evolved a few changes, and this resulted in different types of cells that contained different sets of DNA. While some of these evolved cells eventually split off into entirely new species from the original cells, some others retained a special compatibility that allowed them to share their DNA, and they could mate with each other. Exactly how they started the ability to share the DNA (i.e. physically have sex, since they did not have vaginas or penises at that point) to make offspring is difficult to know for certain, but it may be similar to how some species of bacteria can “conjugate” or stick together to create new DNA combinations. A bit like the thing absorbing nearby cells and acquiring their DNA. So, the two new types of parent cells were able to provide not one, but two sets of DNA for their offspring to choose from. The offspring would typically end up with the most successful (dominant) DNA. This gave the offspring a better chance at surviving and reproducing, and this adaptation was so successful that it developed into fully fledged male and female versions of a single species.