If it's early on or half way through the story - it doesn't matter so long as you got someone interesting to carry on the story without him. - I.e. Psycho - Nobody cares too much when Marion is killed in the shower because we've got Norman Bates and his problems to keep things interesting. If it's at the end of the book ala - the Road. Keep it real enough that the reader will be prepared for anything. The sadness will come when the reader cares about your character. Have it be a struggle. When a reader loves a character - can sympathize with his goals, motives, likes, dislikes - even his flaws. he'll be more moved. If he's cliche they won't care no matter how moving the death throes are.
It helps if you make the reader care about them. It also helps if they don't die arbitrarily or to artificially raise the stakes but that the death means something. That's pretty vague, but there are a lot of variables involved.
If you want it to be sad, their character has to be developed well enough so that the reader feels a connection to them. Otherwise they won't care if the character dies. You can do this by either directly developing the character, or by showing the effect that his/her death is having on the other characters. It's all about character development, whether posthumously through other characters feeling and memories, or through the reader having experienced the majority of the story with this character and developed a relationship with him/her.
Okay. You said this was a MAIN character. That suggests you are fairly advanced in the story at least insofar as this character has developed to the point of being a primary catalyst for events - otherwise, he is not a main character. With that in mind, you must have developed his personal storyline sufficiently that the reader will care about what happens to him, death or any other circumstance. At that point, then, the fact of the reader being sad about his passing is a given. If you do not engender some kind of sadnes in the reader over his death, then you have a problem somewhere before the death. I have a ms wherein a secondary protag, much to my surprise, is killed off late in the story - about 3/4 through. When he was killed off, I had such a backlash from people who wanted me to change the story so that he was not killed simply because they liked him so much. That was something I could not do because that was the only rational solution to his storyline. But even my daughter/beta reader was so upset over my killing him that she wrote in large letter across the page, "MURDERER!" She refused to talk to me for two months other than to glare at me and snarl that epithet. Musta done somethin' right! But that's just the key you want for any character. Make the reader care about him, not the killing. If you can do that, then, when you do kill him, the reader WILL care and WILL be sad because they do care about him.
As others have said, though I will phrase it differently: make sure your reader has a positive emotional investment in the character you're killing. That is probably best done gradually throughout the story, such that your reader has grown to care about them. Interestingly, the opposite is just as true: if your reader really, really hates a character, then you can get them feeling some quite unsettlingly joyous feelings over the character's demise, something one would hope they might be less inclined to feel about anyone's death in real life.
Finally got the books, just starting to plod through! To the original post, as everyones said, it's how you've made the readers feel towards the character. Take dobby from the harry potter books, I was so sad when he died, because he was an adorable character and I loved him to pieces. Or even Boromir from LOTR's at first I was glad but then I was oh so sad. I wont lie I cried he was heroic. A character that I was rather glad died was General Woundwort from watership down. Evil bunny. So it's just how you feel towards the character.
You can make the death seem pointless, which would sting even more than a purposeful death. If a main character dies, suddenly, and without purpose (seemingly), that'd make readers sad. Hopefully.
Like Show said, make them feel for the character. This is very basic. It's important to remember that everything must be done for a reason. You shouldn't do something just because someone else did it, or you think it'll be cool. It has to service the story first. If the action or event fails at doing this, then your scene has failed. I recommend going through some stories where primary characters die, and instead of asking how?, ask yourself why?. How did this event impact the rest of what's going on?
It really depends on both what message you're wanting to send, and how you want to go about sending it. For example, maybe 3 chapters into a story, we see that Person B is the best friend of Person A, but to give the message that anyone can die at any time, we're going to kill Person B. So, I would develop their character some, and give them a few big moments pre-death (even if it's the beginning of the story) to show that they're an important character, maybe saving Person A's life, etc. (Depends on your genre). So then, it really boils down to what message you want to send. Shock value? Kill them, but leave no time for mourning for Person A, that can come later, because Person A's in danger. If you want to evoke sadness, maybe give person B's dying moments with Person A at their side, and give them a final exchange. It really comes down to how you want their death to effect the rest of the story, and what you want it to be the catalyst for.