As vvg said, your sponsor will give you URLs to galleries that have your code already. If you're interested in the technicalities, well, the sponsor (the host) receives your code from the URL and places it in the gallery.
Ex.
this is a gallery -
http://btas.juggcrew.com/26p/?t=1
the same gallery with my code in it -
http://btas.juggcrew.com/26p/?t=1&na...Ix,0,0,0,60866
Different sponsors have different URL formats of passing the affiliate ID, but the principle is the same.