I guess Mary Sue is a matter of balance. I mean it is often said that Mary Sues are "very special, very beautiful, very powerful". But the thing is that the main characters are usually special and attractive too, because, well, they ARE the main characters. They don't have to be absolutely perfect, but they have to have something that will attract readers to them. There is just the balance which is provided by logic.
For example, let's imagine a cute girl character who lives in a village. One day she discovers she has some kind of angelic powers, which are absolutely not usual for their kind.
And the we have some ways to develop the plot
1) Some people get scared of her powers, some like them. Possibly, this is the place, where the girl has to start her adventure to find out what she is. She has to change herself and the other characters appear to make influence on her in any way. And she can change the others too. This is a draft, but more or less fine plot, where we just have our main character. Yes, she has nice appearance and she is very special. But there is a struggle for her, and it is not like everyone fall for her. The plot develops and so does the main character. Her new knowledge, her conclusions, mistakes and wins will attract the readers and make them feel the character is alive.
2) Everyone still loves her. They don't care if her powers are absolutely not normal, they just all love and adore anyway. Most likely such plot will turn into idea of her being one of a kind and also the world savior. She suffers dramatically, fights a lot and saves the world. The other characters are only needed to show that she is much better than all the others. She is the best, of course and have no weaknesses. And even if she has, they somehow turn into her powers too. This is Mary Sue. This is an empty character, who doesn't provoke any feelings for her. Because she is just a picture, not a person. She was created to be the best, she finished being the best. There is no development at all.
3) Everyone hates her. She suffers and tragically destroys the whole village because she lost control while crying or whatever. Then she runs away and suffers, fights and suffers and suffers the whole plot because she is so special and nobody can understand her. As you see, Mary Sue doesn't always have to be loved by everyone. But trying to attract the readers by making the character's life absolute drama is a sign of Mary Sue.
You can make any character you want. It can be beautiful, powerful, it may have tragic life. But it will never be a Mary Sue until you look for the logic and plan your plot wisely.
I'm possibly not the best author of all, but I really do work on my characters psychology and this helps. I'll make an example to illustrate what I do. I have many characters, but I will take the hardest guy
The setting: A usual city, no special powers, no magic, just real life.
One of the characters is Shelby Jade. He is a naive, kind person. He is very helpful, he makes friends easily and many people like him. He is clumsy, but this clumsiness is cute too. He comes from a rich family and he is also very, very clever. Sounds like a Marty Sue (male Mary Sue analogue), right?
Then it is time to add some balance.
Some people often use his kindness and helpfulness to use him. This doesn't always happen, but still this is the problem he has to deal with.I never make a tragedy of this, but use this feature to add some struggles and make Shelby think about trusting other people. However, he doesn't really want to change his mind that easy.
Many people like him, but some are not. For example his colleague doesn't respect him and finds him weak. But again, I'm not making a tragedy out that. The colleague is not shown as monster, but they have opposite point of views. And the best thing I like about that is that none of them is right. The reader is free to judge the main character, to take or to decline his point of view.
Moreover, his good friend Luka sometimes teases him and uses his naivety to trick him.
Shelby's clumsiness has become a reason of some serious fails and troubles. Not a tragedy, but a thing to make the reader to facepalm and say: "Oh, God, why is my favourite character such an idiot?". But the balance is when I don't make him absolute idiot. He still does good things sometimes.
His rich family isn't perfect at all. One more struggle, not a tragedy. I've tried to add some conflicts, but avoided: "You never understand me!" drama thing. This is why Shelby has some tension in his relationship with his parents. But he doesn't make a drama of this, he tries to work thing out. And so does his parents. But their differences make this relationship to fluctuate and change during the plot.
Shelby is clever not "just because". He has some talent in Math, Physics and other subjects, he loves puzzles and this is his hobby to train his mind. It can also be hard for him to explain some things or make people understand him
Finally, the plot twists that way that Shelby is not the only one. He will have to meet two other people from rich families. The girl Sasha is not that smart, but she is very sly and tricky. The guy Victor is very confident and aggressive. And Shelby isn't going to be the best, because Sasha's and Victor's strengths may become his weaknesses.
Shelby is really one of my hardest characters, but a the same time one of the most developed. His base is really hard and he could have turned into Marty Sue, but I worked things out and now people really like him but sometimes ask: "God, why".
I hope I was clear enough with my... wall of text. As summary I'd like to say that any character can be developed good enough. The main idea of Mary Sue is making things absolute. Absolutely evil, absolutely powerful, absolutely special, etc. Mary Sues don't provoke any feelings for them. Readers don't fall for tragic backstories, they fall for character's personality which can be shown while he/she is getting through the troubles. Anyway, no fear of creating a Mary Sue should make you stop. But in case you feel your character turns into one, develop it and work things out.
Here are my personal tips how to avoid Mary Sue:
1) No drama for drama, no perfection for perfection.
2) Make character struggle, not suffer for no real reason. Even the character loses, he should at least do something
3) Make you characters persons, not pictures.
4) Let your character make mistakes and change his/her point of view
5) Let you reader follow and judge the character without forcing to love him/her
6) Let other characters influence your main hero. Show that there are people different from him/her
7) Let the weaknesses be the weaknesses