hi,
Very often, is has to do with Browser Cookies.
Basically, the site that you visit tags you with the cookies of the advertiser/affiliate provider. This can probably be done in a couple of ways.
1) Javascript -> The site that shows your the affiliate program/advertisement also evokes a script that tags you with the cookie, after comparing your unique cookieID with the advertisers' database.
2) iframe -> Same thing as above. Only, the iframe loads the advertisers' ads natively in your browser, and does the cookie implant directly. They will still extract the affiliate referrence from the site that is an affiliate.
3) Possibly other methods I'm unaware of.
As for the "originating" website, it proabably works on the same principles above. Basically, it's up to the advertiser how they want to credit the affiliates. They may choose to ONLY allow the first/originating site credit, by not overwriting the users' cookie (or their own in-house database) with the value of another (competing) site. This way, if a user visits your page (origin), the "credit" stays for a specific period of time, e.g. 1 month. So regardless of how many times the visitor visits other sites, you'll still be accredited accordingly.
Likewise, the advertiser may choose to credit all affiliates for any purchases made if they would like to. The backend combination is almost limitless.
Cheers,
Alvin.