This is great question. Thank you for the opportunity to address it.
I want to first look at the primary verses that deal with the first part of your question:
"Do not judge lest you be judged. "For in the way you judge, you will be judged; and by your standard of measure, it will be measured to you. "And why do you look at the speck that is in your brother's eye, but do not notice the log that is in your own eye? "Or how can you say to your brother, 'Let me take the speck out of your eye,' and behold, the log is in your own eye? "You hypocrite, first take the log out of your own eye, and then you will see clearly to take the speck out of your brother's eye." Matthew 7:1-5
"Therefore you are without excuse, every man of you who passes judgment, for in that you judge another, you condemn yourself; for you who judge practice the same things. And we know that the judgment of God rightly falls upon those who practice such things. And do you suppose this, O man, when you pass judgment upon those who practice such things and do the same yourself, that you will escape the judgment of God?" Romans 2:1-3
In the verses in Matthew, Jesus is dealing specifically with the Jew, and how they treat one another (7:12).
The religious leaders were judging their brethren based on the Law, while failing to see that they also fell short of it. It is only possible for them to see clearly enough to help their brother (the one with the speck), if they first see themselves as self-righteous (the one with the log). We must first see our own falling short, before we can see clearly enough to warn others that they are falling short.
Paul deals with this in Gal 6:1 when he says:
“Brethren, even if a man is caught in any trespass, you who are spiritual, restore such a one in a spirit of gentleness; each one looking to yourself, lest you too be tempted.”
The “trespass” that Paul is speaking of, is not a evil behavior, but rather that they had been led away from the path of trusting in God’s righteousness into establishing their own righteousness through the works of the Law. He is to restore him, lest he be tempted to “fall away” also. Those that are “spiritual” are the ones who are trusting in God’s righteousness, and not their own.
Obviously, in order to restore someone who has strayed from the path, you must judge them as having done so. What is interesting in the verses in Matthew, is right after Jesus tells them not to judge in verse 1, He then tells them in verse 6 not to give what is Holy to dogs or to throw their pearls before swine. How can you not know a dog or swine, if you do not first judge them as such. Are they not the ones with the logs? The self-righteous?
In the verses in Romans, we are now dealing with the Jew judging the Gentile based on the Law, and how the Law condemns them both. Under the law, we are all found falling short of the glory of God. If you want to be under the Law for righteousness, then it is by the Law that you will be judged.
The idea that we are “not to judge” can only be viewed in this context, and not in every context, because we are told many times to judge matters in this life. This brings us to the second part of the question, and what it means to “judge rightly”. Jesus said in John 7:24:
"Do not judge according to appearance, but judge with righteous judgment."
It is not what someone does that determines their righteousness, but in whom they believe.
I have much more to say about this, especially when it comes to judging matters of this life, angels, etc, but I fear that I have already taken too much space.