There are different scenarios to be taken into consideration here.
Exiting to Canada:
This situation is by far the easiest, given the data sharing between the US and Canada.
In many cases (such as VWP nationals without an ESTA), when entering the US by land you will get a paper I94 form stapled into your passport. If you received this form, when entering Canada ask the official to take it out and report your entry into Canada to the US authorities.
If you did not receive a paper I94 (such as if entering the US by air or sea), print the result page of this search and present it with your passport at the Canadian checkpoint. Ask the official to confirm that they will report your departure to the US authorities.
The day after you entered Canada, go onto this website and check whether your exit from the US was recorded. If, for some reason, it wasn’t, follow the steps below "exiting to Mexico by public transport".
Exiting to Mexico by private vehicle/on foot:
Unlike Canada, Mexico does not share data with the US regarding entries and exits of foreigners. Instead, you have to make your way to the US checkpoint, which means parking your vehicle somewhere at the border and possibly walking across the road.
Present your passport and paper I94 form, or, if you did not get one on entry, a printout of the result page of this search, to any present official, and ask them to record your exit from the US. Then hop back into your vehicle and continue your trip.
Exiting to Mexico by public transport:
This is the hardest situation, because the US does not perform border checks on exit and you will not get an opportunity to declare your exit yourself to a US official.
In other words, when exiting the US to Mexico by public transport, your exit from the US will not be recorded.
Consequently, once you get back home from your travels, gather the following materials:
a printout of the result page of this search;
a photocopy of your passport ID page, the pages with the US visa and entry stamp (if applicable);
photocopies of all passport stamps obtained since leaving the US (and in particular the Mexican entry stamp)
all original tickets, boarding passes and bag tags that you still have from the US to Mexico and elsewhere and back home (after making copies for your own records).
A dated bank statement showing all transactions since leaving the US.
Any dated original receipts obtained since leaving the US.
A letter explaining in detail your entire trip, stating the date and place of exit from the US, and asking for the exit from the US to be added to the electronic I94.
Send all these materials by registered mail to this address (as stated on the CBP website):
Coleman Data Solutions
Akron, OH 44306
Attn: NIDPS (I-94)
If the evidence is deemed to be sufficient (which it should be), the record will be updated, so check it from time to time here. Do note that Coleman do not respond to any correspondance.
If, for any reason, the record is not corrected by the time you decide to re-visit to the US, then gather all the above mentioned evidence (except the passport copy and explanation letter) and bring them on your next trip to the US for presentation at the border, so that the record can be retroactively corrected. However, this should only be done as a last resort, because if you're unlucky enough to get the wrong officer on re-entry, you may well be refused entry and blacklisted in the immigration records in spite of any evidence you present (although if you hold a visa, their decision can be appealed to an immigration judge)