Anywhere from 18-21 weeks because that is when they do the anatomy scan. Most times if baby is cooperating, they'll be able to determine the sex at that time.
Usually your Dr checks around 20 weeks. Before that there is a large margin of error since internal sex organs begin developing at 10 weeks and external even later than that.
I was 15 weeks when I found out with my first:))