How and when did sex evolve?
Sex (depending on how you define it), evolved very early (more than a billion years ago). Even species like yeast have a type of "sex" (see below).

Also, this doesn't mean there is necessarily a big difference between the two sexes.

The yeast Saccharomyces cerevisiae, for example, also has two "sexes", called mating types. One is called alpha and the other is called a. They can only mate if they have different mating types, so it is analogous in a way to mammalian sex. However, the genetic difference between these two types is tiny. There's a specific location in the genome that's called the MAT locus. This location can have either an alpha sequence or an a sequence, an that will determine the sex. The rest of the genome will be the same between sexes.

So it really doesn't seem like it is a huge evolutionary step to go from no sex to some simple version of sex.

See more details about yeast mating here.

Also see this wikipedia article on the evolution of sexual reproduction.