Basically it works like this:
On amazon up they put an image or other code on their page that generates a random id and stores it in a cookie.
When you're on Facebook the id is passed back to Amazon which can generate an ad based on your history.
It works because the resource loaded on fb and amazon originates from the same domain. Eg. 3rdpartyretargettingadnetwork.com