Gametes, chromosomes and morphology - usually the first two are enough to determine 99.9r% of people as unambiguously male or female.
The situation isn’t helped by Americans often using the word gender as a direct substitution for the word sex. Gender is the set of social expectations around how the different sexes should look, feel, think and act.
Originally this was rigidly imposed (two genders - males obliged to be masculine / females feminine - or suffer negative social consequences) but now in the West people are free to construct their own gender from a set of infinitely variable gendered characteristics.
[disputed ideological view] Someone’s expressed gender doesn’t alter what sex they are, though - nor is someone’s ‘true sex’ that which their gender identity might suggest. I say this because it doesn’t seem logical to complicate sex (the process by which new humans are created) with the infinite complexity of human personality and culture. Sex doesn’t determine character, and character doesn’t determine sex - would be my TL;DR.