Sex refers to biological differences between males and females. That can include genitalia and genetic differences. It is genetics which defines the sex of an individual. Women have 46 chromosomes plus two Xs and men have 46 plus an X and a Y. The Y chromosome is dominant and carries the signal for the embryo to begin growing testes. Women have higher levels of estrogen and progesterone than they do of testosterone, while men have more testosterone than estrogen and progesterone.
Sometimes, a child is born with a mix between female and male genitalia; they are sometimes termed intersex, and the parents may decide which gender to assign to the child. Intersex individuals account for around 1 in 1,500 births.
Gender refers to the cultural differences expected of men and women according to their sex, which can be defined by an individual's concept of themselves or society. Instead of being only assigned by genetics as sex differences generally are, gender roles are adhered to as an response to family interactions, the media, peers and education.
A person’s sex does not change from birth, but their gender or gender identity can.
In short, "sex" refers to biological characteristics and "gender" refers to the individual's and society's perceptions of sexuality and the physical concepts of masculinity and femininity.