While ultrasound can be performed at any stage of pregnancy for the purpose of finding out the sex it is best done between 18 and 26 weeks.
This method is the most frequent for finding out the sex of a baby, however, it is the least accurate. Accuracy depends on several factors: baby's cooperation, gestational age, technician's ability, etc.
You can read more here.
http://www.childbirth.org/articles/decide.html
If you need anything else please feel free to ask.
