Always the most difficult thing to say, to a native speaker or anyone else: expressions of sympathy. Don't look for idioms or clever sayings at times like these. You speak sincerely from your heart and even if the words are not in the right order, the gesture will be understood and appreciated.
For surviving family members, you might start with a simple, "I am so sorry for your loss." If you know the person who died, you might give a very short endorsement of his life, such as "I thought so much of John. He was such a good nieghbour to us all," or whatever the circumstances of your acquaintance.
Unless the deceased was more than just a close friend, you may not need to express sympathy to the friend for the loss. If they were so close that you might confuse them for a married couple, or brothers or children of the deceased, then of course, treat them that way with sympathy. But if you are speaking to a friend of the person who died, you may speak insted of what a good person / neighbour / colleague he was, how happy he always seemed and made everyone else, any sort of positive thing you remember or know about the deceased.
Frankly, there is little need to say much more than that if you are, for example, at the funeral home or cemetery. If there is a "wake" or party to celebrate the deceased's life, and the mood lightens considerably, then you may wish to add a short comment or two about some particularly good time you shared.