Gender is a social construct that refers to how one relates to the gender categories within one's society. These are often described with values such as masculinity, femininity, or androgyny, and usually involves one's behaviors, attitudes, and appearance. Commonly, gender is assigned at birth, based on one's sex, and one is typically raised with the expectation that one will identify as that given gender. Gender can aligned with one's sex (cisgender) or differ from one's sex (transgender).
All societies have a set of gender categories, which are typically based on a division of labor. In most societies--particularly Western societies--there is a gender binary, meaning two recognized genders (men/boys and women/girls), and those who exist outside these categories fall under the non-binary umbrella. Some societies have gender categories other than men and women, such as the hijras of South Asia. These are often referred to as third genders (and fourth genders, etc.).
Gender often has some relation to gender expression, although gender presentation does not have to strictly correlate with gender (gender non-conforming). Pronouns also typically have some relation to gender identity or gender expression, but does not have to.
Gender vs Sex
Sex is a biological value, generally determined by genitalia, hormones, and/or chromosomes. Genitalia is typically the basis for one's assigned gender at birth. For example, a person with a penis is generally assigned male at birth and is typically raised with the expectation that they will identify as male. If one's sex characteristics differs from male or female it is known as intersex.
It is generally accepted that sex refers to one's physically characteristics, while gender refers to one's internal sense of identity and relation to the gender roles in one's society. Sex and gender do not have to align.
The concept of gender, in the modern sense, is a recent invention. The ancient world had no basis of understanding gender as it has been understood in the humanities and social sciences for the past few decades. In many societies there was no distinction between sex and gender, so "sex" was used to refer to either.
Sexologist John Money introduced the terminological distinction between biological sex and gender, and coined the term "gender role" in 1955. He defined it as "all those things that a person says or does to disclose himself or herself as having the status of boy or man, girl or woman." Before his work, it was uncommon to use the word gender to refer to anything but grammatical categories. However, this meaning of the word did not become widespread until the 1970s, when feminist theory embraced the concept of a distinction between biological sex and the social construct of gender.
In some contexts the two words are still used interchangeably, such as with non-human animals. For instance, in 1993, the US FDA started to use gender instead of sex for animals. Later, in 2011, the FDA reversed its position and began using sex as the biological classification. In legal cases alleging discrimination, sex is usually preferred as the determining factor rather than gender, as it refers to biology rather than socially constructed norms which are more open to interpretation and dispute.
How many genders?
Gender is a infinitely large spectrum, with many positions and identities. A simple way of looking at it is male, female, and everything in between (androgyne). For example, demiboys are slightly, but not fully men. Androgyne people are in between or simultaneously men and women. However, this model is still flawed as it does not encompass the full range of potential gender experiences, such as abinary and atrinary genders.