The structure of teeth consist of inner nerves, dentine (with tubules) and enamel (with porous crystals)
The outer layer of your teeth is called enamel, saliva products bind to the enamel forming a film (the pellicle). This pellicle forms a few seconds after tooth brushing and helps to protect your teeth from acids in foods.
Highly staining products such as cigarette smoke, coffee, red wine (chromogens) etc attach to this pellicle and over time work their way through the porous enamel and dentine tubules (animation to show chromogen particles moving through layers of the tooth) and cause your teeth to appear more yellow.