The drug test tests for the level of THC in your system. You get busted when you're over a certain level. I don't know shit about the chemistry involved, but the more often you smoke the more residual THC you'll have floating around your system. When you smoke you get high, and then most of it leaves your system but some sticks around for a while. So basically if you rarely smoke you can smoke and then pass a test a few days later because you only have a bit left in you, but if you smoke daily you'll always have a bunch in you.
At least, I think this is how it works. There's also something about how it gets stored in fat cells, which is why exercising helps get it out.
Point is, if you rarely smoke you can pass a test a few days after smoking. Assuming you're not smoking a ridiculous amount. The best policy is just don't smoke when you might need to take a test, i.e. if you're looking for a job or you work somewhere that does random testing.