i) you want to know how a person identifies
ii) you're gathering demographic data so you also want to get trans status
At this point there's no point asking about gender. Certainly in the UK asking for sex and then asking for gender is going to be seen as hostile to trans people, while asking for what sex someone identifies as and then asking about trans status is seen as less hostile.
I can't think of a situation where you'd ask for the sex and not for trans status.
Honestly, I don't think you need to ask for any (gender|sex)-information in most cases, but it is often added out of completism. The only valid reasons are: - Self-presentation of the user, e.g. in a profile page -> allow the user to write whatever they want - Advertising, argh, but you often can't get around this. I don't know if advertisers care about how people identify or about trans people, but they probably want a simple m/f flag that fits enough people. - Medical or legal stuff