Etymologically, the word "gender" originally meant kind/type/division - it's closely related to the word "genre" and also "genus".
In English, the first use of "gender" was to refer to the grammatical classifications in various languages - often masculine/feminine/neuter (as in Latin, ancient Greek, proto-Germanic and modern German), in some languages masculine/feminine (as in French) or common/neuter (as in Swedish). It's believed that proto-Indo-European originally distinguished based on animate/inanimate, but at some point this evolved into M/F/N. Animacy distinctions are still used in some languages.
Almost as old, or possibly equally old, in English, according to the OED, was the use of "gender" in its original etymological sense of type/kind, but this died out. For example, in 1662 a writer used the phrase "diseases of this gender" to mean "diseases of this type/kind" - nothing to do with gender as we understand it today. More "genre".
Then, from the late 15th century onwards, we have the use of "gender" to mean biological sex ("males or females as a group; = sex" - OED). So in 1474 a document referred to "his heyres of the masculine gender of his body lawfully begoten", i.e. someone's lawfully begotten male heirs. The OED calls this sense 3(a) and it comments "Originally extended from the grammatical use.. In the 20th cent., as sex came increasingly to mean sexual intercourse... gender began to replace it (in early use euphemistically) as the usual word for the biological grouping of males and females. It is now often merged with or coloured by sense 3b."
So then we get to sense 3b ("Psychology and Sociology (originally U.S.). The state of being male or female as expressed by social or cultural distinctions and differences, rather than biological ones; the collective attributes or traits associated with a particular sex, or determined as a result of one's sex.")
3b is first attested in 1945 in the American Journal of Psychology: " In the grade-school years, too, gender (which is the socialized obverse of sex) is a fixed line of demarkation, the qualifying terms being ‘feminine’ and ‘masculine’." Five years later, another article in the same journal states that a recent book "informs the reader upon ‘gender’ as well as upon ‘sex’, upon masculine and feminine rôles as well as upon male and female and their reproductive functions."