There are a few important things to keep in mind:
Understanding why kids imitate behavior can help you to identify when imitation is more likely to lead to problems down the road.
Whether or not children imitate adults depends not just on the adult, but on the child. That means that what worked for you with one kid (or what worked for another parent with their kid) may or may not work for you with this child.
Kids will hold on to whatever behaviors work well. Adults can influence this by shaping the consequences for different kinds of behaviors.
Many "bad" behaviors are caused by a combination of factors, including developmental stage. Trying to force behaviors that your child isn't developmentally ready for won't be very effective, and can even backfire.
I'll address each of these points in more detail:
Children imitate to learn, and they imitate to belong
Imitating others' behavior is a great way to learn, and we use it throughout our lives. (Think about the last time you were in an unfamiliar train station or airport --- you probably looked around to see what everyone else was doing to know where you needed to drop off your bags, etc.) This is part of a broader phenomenon called "observational learning" and has been widely studied throughout infancy and childhood (here's a classic paper on it). Our ability to learn so effectively from each other rather than having to learn everything first-hand is one of the things that makes us who we are as a species, and it's a go-to strategy in a variety of situations. Whenever a child lacks information about what to do or how to behave, he or she may use observational learning to fill in the blanks. If your child is old enough to talk about it, you can use your own guidance to fill in those blanks instead by explaining what behavior is okay and why. New or unusual situations may be especially important times to check in with your child about your expectations for his/her behavior (and/or make a point of modeling the behavior you want to see) (study).
Imitating isn't just effective for learning, though, it's also a way to bond socially. Infants and toddlers will imitate even strange and apparently meaningless actions (study), and often express happiness just at the act of imitating (study, study). It feels good to do what the people around you are doing. In some cases, imitation can be its own reward.
So imitating behavior in general is not something you would want to discourage (and I doubt you'd be able to even if you wanted to). Here is a nice, approachable explanation of observational learning in children, including a description of some of the classic research on this topic. Children can learn any type of behavior this way, positive or negative.
Some children are more likely to imitate than others
Some children have a strong tendency to imitate the behavior they see and some don't as much. This is actually related to some early indicators of personality, such as how extroverted a child is (more extroverted toddlers are more likely to copy behavior: study).
Strategies that don't work get dropped
The best way to think about imitation is perhaps as a way for behavior to enter a child's repertoire --- whether or not it becomes a stable part of how they continue to behave is a separate issue.
Kids are flexible learners and, as with pretty much any behavior, they'll keep doing what works and they'll stop doing what doesn't.
They often have several strategies/behaviors they can try out, and they'll vacillate between them over time, using the ones that work best more and the ones that work the worst less and less (study).
As a parent, you play an important role in controlling what "works" by your response. Make sure you're rewarding the behaviors you want to encourage and that you're not rewarding the behaviors you want to discourage. Keep in mind that attention, even negative attention like frowning and yelling, can be rewarding for kids, so making a big fuss when they misbehave can actually reinforce that behavior.
Age and maturity matter
Many common "bad" behaviors can't be corrected overnight because their cause is the slow nature of human development itself. A particularly clear case of this is the set of skills known as "executive function" in developmental psychology. Roughly, these are self-control and self-regulation skills.
A big one is impulse control. For example, if you see something you want, you feel the impulse to grab it. That's not always appropriate, so we have to learn to control that impulse and only act on it if its okay to do so. Very young children can't do this --- they literally can't, even if they know they should and they want to --- because their brains simply aren't ready to support that behavior. It's a skill that develops very slowly throughout childhood (and even continues to develop in early adulthood) (study), with lots and lots of practice.
Another important executive function skill is self-regulation, especially emotional regulation. As adults, sometimes we feel overcome by exceptionally strong emotion, but we take for granted how skillfully we deal with more everyday emotion all of the time. We can absorb the little ups and downs throughout the day without much effort because we are skilled and regulating our response to emotion. This is a learned skill, though, and it develops slowly. What seem like tiny problems to an adult can feel like huge tragedies to a toddler (and this contrast has been the subject of lots of comedy, like this or this). Self-regulation is also important for activities that involve concentrating or staying on task. There are very real, practical limits on how long a given toddler can pay attention to one thing, and no amount of parental guidance/support/punishment will change that.
In response to your assertion
...we can't totally get rid of bad or poor behaviors but don't want them to persist while child is psychologically immature.
the answer is "it depends." While it's true that some bad habits can be effectively "nipped in the bud" while a child is young, some behaviors will be very hard to change precisely because the child is still young. Things like fidgeting, inappropriate crying, getting too excited, and giving in to impulses can't be stomped out if the reason they're happening is because the child can't help it. That would be like getting upset with a 3-month-old for wetting her diaper --- she can't control it yet.
I want to be very clear that just because some bad behaviors may be a result of a child's developmental stage doesn't mean you should just ignore it. Parents play an important role in helping their children to learn these difficult skills (study); just because they can't quite do it yet doesn't mean you shouldn't help them try. I just want to emphasize that some "bad" behaviors will require a long time to correct, so have patience (with your child, and with yourself).