The first thing you need to understand is that "gender" is not the same thing as "(which) sex". They are often used interchangeably, and indeed in most people they are intercorrelated to a very high degree, but there is a significant portion of the population for which this is not true.
- sex (as in the attribute, not the act) is most commonly used to mean the physical, genetic, or biological male-ness or female-ness of a person. It is a common misperception that this is entirely caused by the presence or absence of a Y chromosome, though the misperception is understandable because this is certainly true in the overwhelming majority of cases. But there are clear exceptions. "Sex" in this meaning might be less misleadingly called physical gender.
- gender refers to all attributes which are commonly thought of as "masculine" or "feminine" traits. This technically includes "sex", so you might define "sex" as "one's physical/genetic/biological gender". However, even leaving out "sex", there are many separate aspects to "gender". Again, these aspects are highly correlated in most people -- but not in everyone.
The next thing you need to understand is that there are many attributes, both physical and behavioral, which make up what we call "gender". (In other words, gender is not "either-or", or even a "spectrum", but a multidimensional continuum.) These attributes may tend to cluster into two groups which we call "male" and "female", but these convenient labels are inadequate to describe the true situation for many people.  
Gender is therefore a social construct based around arbitrary similarity clusters, not a "scientific fact" or any other such authoritative term. You can draw lines around specific sets of attributes and decide that "male" is on one side of the line and "female" is on the other, but any particular set of lines are going to be highly context-dependent; if scientific rigor is desired, it's much better to speak in terms of those specific attributes (e.g. "has penis" or "menstruates" – and remember that there are individuals who do both!) than in vague terms like "female" or "male".
Note that when we use the terms "male"/"boy"/"man" or "female"/"girl"/"woman" here, we are referring to the particular sets of gender-attributes that apply in each case, not endorsing gender essentialism.
Gender-related attributes include:
- physical / biological:
- chromosomes (XX, XY, or other)
- skin tone (adult females tend to have softer, lighter skin with a layer of subcutaneous fat)
- skin padding (adult females typically have additional fatty deposits in certain areas, giving them a more "curvy" shape)
- body & facial hair (adult males tend to have more hair everywhere except the top of the head, especially on the face)
- genital configuration (inny vs. outy, with a few other variations generally (and questionably!) viewed as birth defects)
- sex hormone balance (mix of androgen, estrogen, and progestens)
- back of the skull (adult males have a bump which children and adult females lack)
- musculature (adult males tend to have greater upper-body strength)
- height (adult females tend to be a few inches shorter than adult males)
- reproductive role
- whether you contribute an egg or sperm
- whether one's body hosts the embryo
- brain structures (which heavily influence behavior and determine gender identity)
- gender identity: what gender you feel like you are in your head (male, female, agender, genderfluid...)
- sexual attraction: males tend to prefer females and vice-versa, but this is reversed for a large percentage of the population ("homosexuality"). Some people are attracted to members of both genders ("bisexual"), while some are best described as "asexual" (not feeling sexual attraction for anyone)
- play affinity: juveniles tend to prefer to play with members of the "same" physical gender; to some extent this is socialized, but it also seems to be a genuine preference -- boys have a notable tendency towards very different and incompatible styles of play than girls do (see next item)
- play preferences: "girls" tend to prefer dolls, role-play involving housekeeping and nurturing, and more gentle games; "boys" tend to prefer sports, role-play involving violence (fighting and killing), and other competitive and kinetic activities
- body map: the internal wiring which tells you what shape your body "should" be also seems to have an opinion about what physical gender traits you should have
- sense: the sense or feeling of which gender you "are" inside your head; it's not clear what generates this, but it seems to be a combination of factors and is sometimes felt quite strongly -- and sometimes in sharp disagreement with physical gender. (This sense is also sometimes referred to as "gender identity", though I am here using that term in a broader way to refer to all the various components of gender, in sum.)
The Gender Continuum
And finally, none of these attributes – not even the physical ones – have clear lines of demarcation. You can place a line in any arbitrary place and declare that anyone on one side of it is female and anyone on the other side is male, but there will always be people who don't fit your expectations.
For example: if you define "male" as "having XY chromosomes", then you would be calling a lot of people "men" who were registered female at birth, raised as girls, identify as female, and visually are clearly women – due to Androgen insensitivity syndrome (photo here).
Various intersex conditions similarly blur the lines with respect to reproductive organs and genitalia.
- World Health Organization: Gender and Genetics
- Trans Student Educational Resources: The Gender Unicorn (infographic)
- 2021-09-15 Why Y Chromosomes Won’t Be Around Forever (12:05 video, SciShow): sex determination happens in many, many different ways
- 2019-09-05 Biased sources, motivated reasoning, and blithe assumptions: the TERF story
- 2018-11-13 We Finally Understand That Gender Isn’t Binary. Sex Isn’t, Either.
- 2018-01-05 If you're multicellular, you can't help but be mosaic
- 2017-09-01 Beyond XX and XY: The Extraordinary Complexity of Sex Determination
- 2017-04-28 A former 'MythBusters' extra thanks Adam Savage for crushing a harmful myth about gender. (via)
- 2017-03-03 Facebook post via Google+ listing some of the many variations of biological gender
- 2016-04-17 Gender Identity and Associated Gender Language: A Primer
- 2015-12-23 Scientists discover 'genderfluid' lioness who looks, acts and roars like a male (via): yes, Virginia, gender fluidity does exist in nature (...although I'm not sure that's really the right term here... and the article states that she "grew a mane" and "developing a deeper and more masculine roar" as if it was a conscious decision, which seems unlikely -- more likely her hormone levels are unusual for a lioness)
- 2015-12-15 Sex beyond the genitalia: The human brain mosaic
- 2015-09-20 The village where boys are born as girls: Genetic deformity means Dominican Republic babies are born apparently female and only grow male sex organs at puberty - the boys in question frequently say they "never felt like girls" and tended to play with boys, illustrating yet again that gender identity is neither acquired nor based on one's apparent physical sex
- 2015-03-22 Neuroscience Proves What We’ve Known All Along: Gender Exists on a Spectrum
- 2015-02-18 Sex redefined (via)
- 2015-02-15 "The idea of two sexes is simplistic. Biologists now think there is a wider spectrum than that." "...my only quibble would be with that “now”. You’d have to define “now” as a window of time that encompasses the entirety of my training and work in developmental biology, and I’m getting to be kind of an old guy. Differences in sex development (DSDs) are common knowledge, and rather routine..."
- 2015-01-07 Networks of the brain reflect the individual gender identity
- 2016-10-31 Gender Analysis 02. The Gender Axis of Evil (video by Zinnia Jones)
- 2014-09-20 When no gender fits: A quest to be seen as just a person
- 2014-03-20 I'm curious as to why you seem so certain that gender is not a social construct. Answer: It's partly social and partly biological.
- 2014-02-20 Sex differences are real but not binary
- 2014-02-07 Understanding gender diversity: sex and gender are not the same thing
- 2012-03 The Genderbread Person: an educational graphic
- 2011-12-14 Bilaterally Gynandromorphic Chickens, and Why I’m Not "Scientifically" Male
- 2013-04-20 The Tangle of the Sexes
- 2013-02-05 Study debunks notion that men and women are psychologically distinct
- 2013-03-18 Richard O'Brien: "I'm 70% man"
- In theory, this could be separate from the mechanics of intercourse (who does the injecting and who does the hosting), but I don't know of any examples.
- When that sense is at odds with one's physical or socially-perceived gender, this causes an intense discomfort called gender dysphoria.