What if I don't know the parents/partners/etc. personally? It does not matter. It is still appropriate -- I will go so far as to say that it is good -- to condole with them upon their loss. What if I know the family/partners/etc. personally and don't like them? Still. Appropriate. Really.
I don't want to intrude. People, a card or letter is a piece of paper (or, in this modern age, a set of pixels -- but err on the side of paper unless you know the bereaved well or primarily through the internet). They can put it aside and look at it later if they don't feel up to dealing with such things now. You need to write the letter, if you choose to write the letter, in such a way as not to be obtrusive or get into grief poker. But the existence of the letter itself will almost certainly not be obtrusive.
All right, so how do I write it that way? Start with the reason you are writing: "I am so sorry to hear of your loss." "I am writing to extend my sympathy to you in the loss of your mother." "I am writing to condole with you upon the death of your dear friend and cousin, Bernie." Etc. Remember that you are sympathizing with their loss. If this was not the case, you would not be writing. You are certainly allowed to claim that you share their grief. Under no circumstances should you imply that your grief is the greater. Ever, ever, ever. If you cannot avoid grief poker, do not write. If you ever say something like, "I think I was much closer to your sister than any of her blood relations," you have just failed in your mission. You are out to share sorrows, not create them. But expressing that you also feel great sorrow is good -- it's why you're writing. Just don't get into comparisons.
If you do not know the person to whom you are writing, or if you don't think they will remember you well in the confusion that can accompany a great loss, tell them how you knew the deceased. "I was in college choir with Desiree and had kept in touch with her ever since." "I have worked with Loretta for five years, and we shared a cube." "Though I never had the honor of meeting Anastasia, her songs touched me for many years."
If you have appropriate anecdotes about the deceased, share them. "Appropriate" varies according to your audience. If you don't know the bereaved very well, starting a story with, "Your daughter Josie had just left about a dozen shots of slightly used tequila on the sidewalk outside the bar, and this dude comes up to her and says..." is probably not your best bet, no matter how well you feel it captures Josie's wit and joie de vivre. (On the other hand, in some families this is exactly the sort of story that will make them smile through tears and say, "Yah, that sure was Josie!") Adjectives and general traits are also fine at this point: "Great-Aunt Beatrice was always so kind, and she made sure we children knew that she loved us very much. I always looked for her snickerdoodles at family picnics. Her smile lit the whole room." (Do not fear cliche if cliche is what you have. Better to say, "Her smile lit the room," than to let the letter never get written.)
If you do not know the deceased -- say, for example, that your co-worker's grandmother has died -- you keep it short and stick to what you do know. "I know how you loved your Saturday afternoons together," or "I know you will miss her dreadfully."
After you've told stories or described or whatever, it's time to close things off. "Again, words cannot express how sorry I am." "My thoughts will be with you and your family." If you know that you and the bereaved share a religion, now is the time to mention it; if you don't know one way or the other, remember that a condolence letter is not an evangelism opportunity for any particular belief system, and that while some people will, in a state of grief, be able to parse your religious well-wishes into something they can deal with, others will not. If you don't know, best not to get too specific. "My sincere hopes that your sister Lucia was among the 144,000 elect": no.
Do not be negative about anybody. At all. Ever. Not the deceased, not the bereaved, not people of your mutual acquaintance.
You are not required to be eloquent. You are not required to be a skilled worsdmith. If you feel that you just have no way to put your sympathy and grief into words, writing, "I am so very sorry. Word fail me. Sincerely," on a card will still "count." On the other hand, even if you aren't the best prose stylist in your particular zip code, if you can manage to write an actual letter, even a short one, please do. I have never run into a family annoyed by the fact that other people cared about their loved one and took the time to say so personally. If you have a poetic genius that is moved in this situation, by all means use it; but if your muse is silent, "I'm so sorry," has served and will continue to serve when we've all gone.
If there is more than one close bereaved person, you may end up writing more than one letter. They can have substantially similar content. You will not be graded on originality here. You will not receive rejection letters from the bereaved on your condolence letters, even if they are editors.
Anybody with other condolence letter tips, please join in here. I have deliberately kept the examples female as my own mental distancing mechanism, so that I didn't have to think about what I will write when I sit down to write to Mike Ford's family of origin when the address is available and to elisem. And yes, I am writing a paper condolence letter to elisem, even though I've seen her all week and talked to her and will hand-deliver it. I am a strong believer in condolence letters. They are a source of comfort, one of the gentle skills. But one thing they are not is easy. Many of them arrive with the ink smudged in a spot or two, or the paper blotted, because sharing vivid memories is hard, and thinking about one's own and others' pain is hard. Hard, but worthwhile.
(Please note: I am not telling you that you have to do this. I am not, as they said in the second grade, the boss of you. I'm just saying that you should not refrain from sending condolence letters out of fear that they will be inappropriate or because you don't know how to do it.)