KRyan says a lot of important things, but I want to add some personal experiences to directly answer the question of how I, as a D&D player for over 15 years, deal with the issue of attachment to my characters.
Disclaimer
I tend to play in RP-heavy groups; that is, groups which do take the time to build backstories and have roleplaying interactions with one another within the game. We also tend to play in settings where resurrection is possible, so that if a character dies, it doesn't mean the player has to roll up a new one. Some groups do it differently, preferring to build characters by numbers rather than story, and/or play in permanent-death worlds.
As KRyan suggests, it's a good idea to find out approximately where on this spectrum your group falls. If everyone else is playing builds, with no more backstory than "My parents died and left me this sword, so now I'm roaming the earth", then there's little point in creating a character. On the other hand, if everyone else is playing characters, it's much more fun to have a character of your own that's more than just a collection of stats.
Dealing With Attachment
The most important thing to remember is that it's just a game. If your character dies, it's not a reflection on you the player. It doesn't mean that you did anything wrong for investing time and energy into creating a character only to have them die. It's just part of the game. Also, a character's death doesn't have to mean that your investment in their story is "wasted". You can reuse a character, wholly or in part, in other campaigns or with other groups.
For me, at least, this is part of the fun. Play a rogue and she dies? I'll remember that I liked the rogue's sneakiness, but not so much its frailty, so maybe I'll try to incorporate the sneaky roguish bits of the first character into a more sturdy character for my next run. I don't feel like I've "wasted" any of my characters, even the ones I played in one-shots, or which died relatively quickly. I see them each as part of the story they were in, as well as practice for the next character I build.
This mindset allows me to get attached to my characters while I'm playing them, so that I feel bad when they're hurt and happy when they're victorious - but to keep just enough distance that while I'll be upset if they die, it'll be temporary and I can move on to building the next one.
Talking to the DM
Another important thing is to have an open line of communication with your DM. If you're starting to get attached to your character to the point where you know you'd be upset if they died, let the DM know. The DM's job is to make the game fun for everyone, so they should be willing to work with you on this. That doesn't mean fudge dice rolls so that your character never dies, just that if things do go badly, you have a way to recover - whether that's through a simple casting of Resurrection, or by going on a quest to the lost Temple of Life on a long-shot attempt to revive your dead character.
For example, in the game I'm DMing right now, the world has very limited resurrection. My players knew this going in, and had already gone on one difficult quest to revive a fallen party member whose player had wanted to keep his character. A few sessions ago, two players had a string of bad luck during a boss fight, and then a monster made an attack that would have killed them both. One of them had already pinged me privately to say that he was okay with his character dying if that's how the dice went; and before announcing that the characters were dead, I asked the other player privately as well. He, too, agreed that he was okay with his character dying.
If either of them had said no, they didn't want their characters to die in this fight as the victims of a series of poor rolls, I would have found a way to keep them alive - either by fudging the damage dice publically, or by invoking a deus ex machina (which are normally bad for drama, but in this case, would have been acceptable to keep the fun for the players). My players were willing to explain to me how they felt about their characters, and I as the DM was able to look for ways to accomodate them, whichever way they chose.
TL;DR
Remember that D&D is just a game, and character death does not mean that the character concept is forever lost. Communicate with your DM and the rest of your group to find a solution to character death that works for all of you.