You can (must, really!) be passionate about what you write, which inevitably ends up with you being emotionally attached to and invested in it.
But you gotta be Abraham with Isaac. Love it, but willing to sacrifice it in the name of good story telling. Doesn't mean you care about it any less, you just have to be brave enough to be mean to it in SPITE of that fact.
I would argue that it is a learned skill as a writer to step back, examine your work critically, admit that something that will make you sad for your characters or plot will actually IMPROVE the story, and do it anyway.
Finally, this is why we have workshop groups and beta readers. Other people can look at our stories and see what we can't (since we are biased, not only are we attached to our work, but we have other "blind spots", for instace, knowing how everything is supposed to unfold already makes it harder for an author to tell what a normal reader will pick up on in the text and what they will miss. Learning to estimate this is, again, a skill that must be practiced. And good beta readers should pick up on it if you don't.). If a bunch of good writers look at your story and say "y'know, after the first act, this one character doesn't help the plot much and if you KILL them it will create great conflict for the other characters" but you love that character SO MUCH... at least now you've had it pointed out to you, you can hopefully think critically about their feedback and determine whether or not the ONLY reason you are keeping that character around is because you like them.
So in a nutshell: The trick isn't to stop being attached to your work, it is in practicing a) the ability to step back and examine it objectively anyway, and b) the willingness to make necessary changes even if they bum you out.
I've heard of writers who cry writing a scene in which they kill someone or do something terrible to a character. But they do it anyway.