Some definitions: Since we're talking archetypes, I have no scruples basing the following discussion on the concept of the Hero's Journey. (See Chris Vogler's discussion for my favourite interpretation.) Definitions and etymologies are taken from the englisch wiktionary. I add this answer since I have the feeling that people use the term "anti-hero" in various ways. This post is meant to clarify what an anti-hero actually is.
- The protagonist: from Greek, "first actor". The character that drives the story forward.
- The hero: Several etymologies -- from Old French over Latin to Greek and an old prototypical Indo-European language, "protect, serve". In Greek drama, a hero is a character that serves others. S/he's altruistic and values the communal good higher than his or her own. S/he's willing to sacrifice him- or herself for others. In other, more narrow senses, a Greek hero is character that participated in the Trojan war, or a god or demi-god that was revered in his or her own shrine.
- The antagonist: From Latin and Greek, "anti" (Greek, "against") and "actor". An antagonist preserves the Status Quo by keeping the protagonist from driving the story forward. Unlike the anti-hero, the antagonist has a well-defined goal, that, however, is opposite to or in other conflict with the hero's goal. The protagonist must deal with the antagonist, if the story is not to stagnate. Chris Vogler draw the following comparison: The hero and the antagonist are forces that want to drive the story into different directions. Think of them as horses that try to carry a cart into opposite directions.
- The villain: From Old Latin, a serf or peasant, someone who is bound to the soil of a "villa", the antique equivalent of a plantation. Interestingly, the original meaning of this word has very little(*) to do with today's meaning. Returning to Chris Vogler's simile of antagonist and protagonist as characters that pull the story into different directions, the hero and villain represent the different possible solutions of the story. They are trains heading towards each other. The villain has the potential to destroy the hero. -- In terms of basic archetypes, the villain is the manifestation of the hero's shadow -- any character trait that can potentially destroy the hero and must be dealt with. The whole purpose of telling the hero's story usually is to show how the hero faces his shadow and integrates it into his or her personality. This psychological dimension of the struggle between the hero and the shadow can be more or less obvious in a story, but in the end, the hero "defeats" the shadow. The shadow is no longer able to destroy the hero. (I make a distinction between the shadow and the villain here, since the shadow does not always need to manifest as separate character.)
- The anti-hero: "anti" plus "hero". The defining feature of a hero is that s/he acts to protect others. In the simplest possible definition, the anti-hero hence doesn't. This does not necessarily mean, that the anti-hero is selfish or that s/he actively strives to harm others; it simply implies that the anti-hero is not heroic. Maybe this is because s/he's not part of a community; maybe s/he doesn't have the means; maybe s/he's not aware that help is required. Any reason that keeps a character from acting in an heroic way turns him or her into an anti-hero.
In the framework of the Hero's Journey, the protagonist usually evolves into a hero. The protagonist sets out with a well-defined goal, undergoes a transformation, and returns in the role of the hero. His or her personality has changed, usually for the better.
The key feature of a hero, in my opinion, is that s/he recognizes the need for change and actively tries to establish this change.
The negation of this concept is a character that refuses to change or participate in it. This definition encompasses the narrower definition above, of an anti-hero as a characters that does not protect others: If s/he is not willing to face the challenge that s/he's presented with, s/he does not have the opportunity to evolve into a hero.
Characters differentiate into heroes and anti-heros when they decide whether they take on the challenge at the core of the story or not. In terms of the Hero's Journey, this challenge is presented in the form of the "Call for Adventure". While both the hero and the anti-hero may engage in a "Refusal of the Call" -- a clever hero will know that heeding the call is dangerous and will be reluctant to actually follow it --, only the hero will finally "Cross the Threshold" and accept the challenge. The anti-hero, on the other hand, walks away from the challenge.
This principle is variable, of course. Some heroes are passive throughout the first half of their story; to cross the threshold was not their explicit decision, but rather, they didn't have a choice. In the end of the story, however they have faced the challenge and mastered it. They have grown. At the same time, the anti-hero still idles away in his or her old position and hasn't changed a bit. In a sense, a piece of narration that tells of an anti-hero is an "anti-story" -- since, really, nothing happens. (Of course an interesting story could be about a character that refuses to change while his entire world turns upside-down. That would be a valid, and possibly very effective, story that however revolves around an anti-hero.)
Now, with this definition in mind, what can we conclude about the characters you list as examples of anti-heros? Unfortunately, I only know Tony Stark well enough to discuss him, but with Tony, it's really quite simple: Tony emerges as a hero. Yes, he is flawed and peculiar (this is what makes him interesting and relatable) -- but at the end of day, he has, undoubtedly, evolved into a hero. He has heeded the call to protect people he cares for and is willing to sacrifice himself. He's a storybook hero, really.
An important thing to keep in mind when developing characters along the lines of an archetype is that characters can switch roles throughout the story. A passive hero might appear to be an anti-hero for a while. A trickster might turn out to be a villain. A shapeshifter is changing constantly, anyways, and a hero -- i.e. a character that already completed the Hero's Journey, which is usually not the protagonist -- might turn into a mentor or even into a villain at a later point of a story.
Overall, the goal of the Hero's Journey is to transform a character into a hero. That explicitly means that the protagonist, at the beginning of the story, is not a hero yet. By the end of the story, however, s/he will be.
Since stories are generally about change, investing too heavily into an anti-hero in the sense outlined above is usually not a smart move. Your audience generally is not interested in a story in which nothing changes. Anti-heros have their place in literature and popular culture, but I would argue that you should only engage with them if you have a very good reason to do so.
Conclusion: How to write a good anti-hero?
People do not generally root for an archetype and condemn another. We can cheer for the villain of one story and the antagonist of the next, and sometimes be horribly bored by the hero. (Think Pirates of the Caribbean. Will is the hero, but everybody loves Jack, who's not changing and who's certainly not promoting the greater good. Stil, his wit and charm appeal to the audience more than Will's blunt "goodness" of the first movie.) Whether we like a character or not does not depend on the archetype s/he refers to, but on whether or not the character resonates with us. In that sense, the question is not how to write a good anti-hero, but how to write a good character in general. (Although what "good" means in that sense is open to interpretation. For me, the qualities "interesting", "realistic", and "complex", are much more important than the generic "good" in the sense of "kind" or "heroic".)
And here's the quintessence: People are different. I don't believe there's a single good way to write a universally appealing anti-hero or character in general.
A good way to start is to analyze the qualities that appeal to you(**), personally, when developing a new character. What do you like? What can you root for? Chances are, if you condense these qualities and pour them into a well-rounded characters, other will root for this character as well.
(*) A wild association. The hero is the character that successfully changes in a story. The villain, always a potential hero in his or her own right, fails. In this sense, the villain is "bound" to his or her Status Quo. S/he's unable to accomplish the transformation that is mandatory for a hero. Not unlike a serf that is bound to a specific patch of soil, the villain is unable to journey as the hero does and fails to change his or her world. Interestingly, this always makes the villain an anti-hero, but not all anti-heros are villains.