How do chefs know when to start cooking each dish in a restaurant?
Different restaurants use different methods. In some cases, they may rely on the server to pace your meal. In this case, the server enters the order all at once, but then returns to the POS system to hit a button to "fire" the next course after the appropriate delay, which sends it to the kitchen to be prepared. Presumably, some POS systems (particularly for major chains with extensive processes) would now be smart enough to handle that delay automatically--but I'm not specifically aware.

In other cases, the ticket comes into the kitchen all at once, and the kitchen is responsible for timing courses. The expeditor or "expo" is in charge of getting food out at the right time, and coordinating to make sure food coming from different cooks/stations in the kitchen all gets delivered to the same table, together (if necessary) or in the right order. In this model, Expo receives the ticket, reviews the items and calls out to fire items at the right time. In this case, Expo can introduce the appropriate delay between courses. Expo also doesn't have to do this alone. They generally work the window where food passes from kitchen to dining room staff to be run to the table. This allows Expo to coordinate with a tables server to adjust timing if a table is eating particularly fast/slow. 

One note on this detail of your question:


  if they are cooking a dish from scratch its impressive its ready so quickly.


Commercial kitchens that cook from scratch don't do end-to-end cooking at the time you order food. Prep is integral in commercial cooking, and ensures food is cooked quickly. Earlier in the day, things are washed, chopped, sliced, etc. Individual components are created, and sometimes even pre-portioned. Things may even be par-cooked if they tolerate par-cooking and being held in the refrigerator (ex, certain noodles). When you order, it's often more of an assembly, often with just the primary protein being cooked at moment. 

Consistent prep also helps make cooking time consistent. This allows for the whole restaurant to know exactly how long items take to get to your table--which gets to your original question of how they manage to pace courses from the kitchen so you get your entree at just the right time after your appetizer.