During mammalian development, the female reproductive system arises with the formation and coalescence of the Müllerian Ducts to form the Fallopian tubes, uterus, and vagina. At their posterior end, the Müllerian Ducts do not open into the urogenital sinus, but rather they form a tubercle at the presumptive opening of the vagina. During the development of the uterus and vagina, these organs are separated from the urogenital sinus by an epithelial plug which eventually thins and perforates to form the hymen.
The hymen is simply the vestigial membranes of the vagina and urogenital sinus at their point of fusion. During development of the mouth, a similar vestigial membrane occurs, called the oral plate, which ruptures as the mouth continues to form. As a vestigal organ, it is difficult to place an adaptive purpose on the hymen. The evolution of a system is the sum of positive selection, negative selection, and no selection, i.e. if a structure is not detrimental to the system, it is usually maintained, even if it serves no purpose. If there is no evolutionary pressure against incomplete fusion of the vagina and urogenital sinus, then the structure persists as a vestige of the Müllerian Ducts. To my knowledge, all marsupial and placental mammals have this structure, so there is probably no specific courtship advantage to virginity, and its persistence throughout Mammalia suggests that there is no disadvantage, or it would have disappeared long ago.