See the information in Apple Trade In - Apple
Note in particular the "Frequently Asked Questions" near the bottom of that page.
One of the things in that section is the following (but also check the rest of the section):
If you accept the trade-in estimate online when you purchase a new Mac, iPhone, iPad, or Apple Watch, we’ll arrange for you to send us your old device. Once we receive it, we’ll inspect it and verify its condition. If everything checks out, we’ll credit your original purchase method and send you any remaining balance on an Apple Store Gift Card via email.
Also, when ordering online, after you click the "Buy" button to start the selection and ordering process, note the following near the top of the screen: