How do doctors determine the due date?

I know they can determine the due date by the first day of your last period or by measuring the baby by ultrasound. When I first had a sonogram, I was seven weeks and six days, but I believe that dr used the first day of my last period to determine my due date because I was one week preggo on the last day of my period. Later I went to a diff dr, and they determined it by the ultrasound. However, they told me I was one week farther along than I thought. When I counted the weeks back, I was one week in the middle of my period. Could it be I got pregnant right before my period came for the last time? Or are they way off w the weeks bc I do know some family members who have got their period while pregnant still?