How do companies like Amazon track what products are most frequently bought together?
This is called Association Learning. Quoting Wikpedia:


  Association rule learning is a method for discovering interesting
  relations between variables in large databases. It is intended to
  identify strong rules discovered in databases using some measures of
  interestingness


So, your question asks about how companies like Amazon and Walmart know what products are frequently bought together. So, that is done by the Apriori algorithm, which falls under the class of Association Learning algorithms.

Again, quoting Wikipedia:


  Apriori is an algorithm for frequent item set mining and association
  rule learning over transactional databases. It proceeds by identifying
  the frequent individual items in the database and extending them to
  larger and larger item sets as long as those item sets appear
  sufficiently often in the database.


This algorithm generates rules which show what items are bought along with which items, depending on the support and confidence thresholds. (As you didn't ask about the particulars, I would skip the details. Pl check wikipedia for the finer details and the algorithm itself).

The arules package of R implements the apriori algorithm. Unfortunately, there is no fast implementation of the same in Python.