"When do I write it's and when do I write its?"

The thing to remember is that it's is a contraction of either it and is or it and has. In English, we write all contractions with an apostrophe: I'm, aren't, you're, don't, etc. This means you can write it's only if you're able to rephrase your sentence with it is or it has, no matter how stiff that makes the sentence sound: it's a shame--it is a shame, it's time--it is time, it's fine--it is fine, it's happened--it has happened.

Its without an apostrophe is a pronoun or a possessive adjective. This just means that we use it like other words such as my, mine, your, yours, his, her, hers, our, ours, their and theirs. That is, we use its before a noun or, less commonly, to replace a noun. Here are some examples: There was problem and no one can explain its cause (i.e., the cause of it); Its cost (i.e., the cost of it) was too much; I don't understand its meaning (i.e., the meaning of it). Notice that in each of these examples if you try to say it is for its, the sentence won't make any sense. This is a surefire rule for knowing when to use it's or its: if you can't rephrase the sentence with it is or it has, write its without the apostrophe.