Sex is a person's biological category, usually — but not always — straightforwardly male or female based on genes, hormones, and anatomic structures.
Gender is the socially constructed side of masculinity or femininity. For example, long hair might be considered a female trait in one culture, but not in another.
Orientation describes a person as being attracted to the same sex, the opposite sex, both sexes equally, or as being at any point along this range.
In a world we don't live in, every person's genes, hormones, and anatomic structures would line up as clearly male or clearly female. Every biological male would conform to a universal view of masculinity; every biological female would conform to a universal view of femininity. Finally, every biological female would be attracted exclusively to biological males, while every biological male would be attracted exclusively to biological females.
Real life is more complicated.
The first use of 'sex' recorded in the Oxford English Dictionary is from a fourteenth century Wycliffe Bible in which Noah was asked to collect "male sex and female" for the ark. Dividing up members of a species according to reproductive function was its primary usage until the early twentieth century when it became the word of choice for physical intimacy (and 1912 marks the earliest recorded use of 'sexy' to describe a person).
For a variety of reasons, a human may not clearly fall into the typical male or female biological categories. 20/20 produced a helpful introduction to intersexuality, as it's called.
'Gender' has its deepest roots in the general notion of kind or category. It is related to the words 'genus' and 'genre.' Grammatical gender, e.g. "la mesa" (the table) or "el caldero" (the pot) was the main specific application of the term. Obviously, grammatical gender is not tightly integrated with biological sex, unless you know something about tables and pots that I don't!
With the rise of 'sex' as the term for physical intimacy, 'gender' became a popular replacement when speakers wanted to talk about (biological) sex without calling to mind sex-the-activity. (As the joke goes: "Sex? Yes, please!") It's still common for these words to be used as pure synonyms.
However, there is a growing trend of treating 'sex' and 'gender' as distinct attributes, a trend which started among United States psychology professionals in the 1940s. Here is a contemporary definition from the American Psychological Association:
"Sex is assigned at birth, refers to one’s biological status as either male or female, and is associated primarily with physical attributes such as chromosomes, hormone prevalence, and external and internal anatomy. Gender refers to the socially constructed roles, behaviors, activities, and attributes that a given society considers appropriate for boys and men or girls and women. These influence the ways that people act, interact, and feel about themselves. While aspects of biological sex are similar across different cultures, aspects of gender may differ."1Whether we use the word 'gender' or some other term to refer to cultural masculinity/femininity as opposed to biological masculinity/femininity, this is a very important distinction. We need some way to talk about it.
Since gender is a cultural category, a person may not feel they belong to the gender their society assigns to their sex. But this is not much different from a New York resident failing to identify as a Yankees fan.
'Sexual orientation' is another twentieth-century term. To quote the American Psychological Association again:
"Sexual orientation refers to an enduring pattern of emotional, romantic, and/or sexual attractions to men, women, or both sexes. […] Research over several decades has demonstrated that sexual orientation ranges along a continuum, from exclusive attraction to the other sex to exclusive attraction to the same sex[....]
Sexual orientation is distinct from other components of sex and gender, including biological sex (the anatomical, physiological, and genetic characteristics associated with being male or female), gender identity (the psychological sense of being male or female), and social gender role (the cultural norms that define feminine and masculine behavior)."2Most people are right-handed; most people are predominantly attracted to the opposite sex.
The Recognition of Culture
What happened in the twentieth century to prompt these changes in the way we talk about sex, gender, and orientation? That's a topic for whole books, but I'll hazard a guess: it was the growing recognition of human culture as distinct from the findings of natural science.
In other words, pink is not really a girl color. That's just an advertising campaign.
1. From http://www.apa.org/topics/sexuality/transgender.pdf
2. From http://www.apa.org/helpcenter/sexual-orientation.aspx