Does the scheduled departure time refer to leaving the gate, leaving the ground, or something else?
Typically it's when the cabin door closes or when the parking brakes are disengaged.
Similarly, does the scheduled arrival time refer to the landing, arrival at the gate, or something else?
Typically it's when the parking brakes are set.
Parking brakes are a popular choice since they can be tracked automatically and it has only limited amount of wiggle room for interpretation. "Gate arrival" and "Gate departure" can mean different things to different people. This is important since the exact definition can have legal and regulatory consequences (pilot hours, delay compensation, final decision authority, etc.)
Airlines determine these times simply by running statistics. They track the time on every route and look at the distribution of actual flight times (as defined above). They then choose a time so that maybe 80% will be on time and 90% within 10-15 minutes of on-time (depending on the shape of the distribution)
It's NOT the average or median flight time: otherwise half of the flights would be late. In fact its quite "normal" for a flight to be early. This also plays a factor in airlines setting minimum connection times which can sometimes feel ridiculously low.