There are many good sermons and teaching around on this.
The Purpose of the Law (Gal 3:19-26) — The Fellowship
"the law has no power to give life."
While it is good that the law reveals our sin, there would be no point in God commanding us to do something that was too difficult to keep for the purpose of showing us that it is too difficult for us to keep, especially when there has never been a need for us to have perfect obedience, but rather God said that it is not too difficult for us to keep and that obedience to it brings life and a blessing (Deuteronomy 30:11-16), so we are capable of keeping it. In Deuteronomy 32:47, it is our very life. In Proverbs 3:18, she is a tree of life for all who take hold of her. In Matthew 19:17, Jesus said that the way to enter eternal life is by obeying God's commandments. In Exodus 33:13, Moses wanted God to be gracious to him by teaching him to walk in His way that he might know Him, and Israel too, in 1 Kings 2:1-3, God taught how to walk in His way through His law, and in John 17:3, eternal life is knowing God and Jesus. So God's law does have the power to give life and the way that I reconcile these verses with Galatians 3:21 is by recognizing that faith in a key ingredient to everything in the Bible, so the law does not bring life apart from faith, but does bring life through faith. In Matthew 23:23, Jesus said that faith is one of the weightier matters of God's law.
In regard to Galatians 3:18, we do not earn our inheritance by obeying God's law because it was never given as a means of doing that, but that does not mean that we don't need to obey it for the purposes for which it was given. In Genesis 18:19 and 26:4-5, the promises are brought about because Abraham walk in God's way in obedience to His law and taught his offspring how to do that, and in Deuteronomy 30:15-16, those promises are again brought about by his offspring walking in God's way in obedience to his law. In Acts 3:25-26, Jesus was sent as the fulfillment of the promise in order to bless us by turning us from our wickedness, so again the promises are brought about by walking in God's way in obedience God's law, especially because Jesus is the living embodiment of God's way (John 14:6).
In regard to Romans 5:20, it is referring to the law of sin, not to the Law of God. In Romans 7:7, Paul said that God's law is not sinful, and in 7:22, he delighted in obeying God's law, however, Romans 5:20 is referring to a law that is sinful, and it would be absurd to think that Paul delighted in causing sin to increase.
In regard to Galatians 3:19, Jesus did not begin his ministry telling people that the law has ended now that he has come and they need to stop repenting, but rather he called for people to repent for the Kingdom of God is at hand, so Galatians 3:19 should not be interpreted in a way that undermines his ministry, but rather all of God's righteous laws are eternal (Psalms 119:160).
"The law was not given to give life, and righteousness was not from the law."
In Romans 2:13, only doers of the law will be declared righteous, so while we do not earn our righteousness by obeying the law, righteousness nevertheless only comes through choosing to obey it through faith.
In regard to Galatians 3:22, the word used refers to the law protecting us by guarding us, not to being trapped in prison that we want to break free from.
"the only way out of the prison is the grace available through the promise by faith."
In Psalms 119:29-30, David wanted to put false ways far from him, for God to be gracious to him by teaching him to obey His law, and he chose the way of faithfulness, so this has always been the one and only way of salvation by grace through faith, not a way of escaping God's law.
In Matthew 7:23, Jesus said that he would tell those who are workers of lawlessness to depart from him because he never knew them, so the real purpose of the Mosaic Law is to teach us how to know Jesus, which is again eternal life (John 17:3), or in other words, knowing Christ is the goal of the law (Romans 10:4). Likewise, in 1 John 2:4, those who say that they know Jesus, but don't obey his commands are liars, and in 1 John 3:4-6, those who continue to practice sin in transgression of God's law have neither seen nor known him. Jesus spent his ministry fulfilling the law by teaching his followers how to obey it by word and by example, so the law leads us to him because it teaches us how to know and be like him, but does not lead us to him so that we can reject what he taught and go back to living in sin.
Galatians 3:26 for in Christ Jesus you are all sons of God, through faith.
In 1 John 2:6, it says that those who are in Christ are obligated to walk in the same way he walked, which in obedience to God's law, and in 1 John 3:10, those who do not practice righteousness in obedience to God's law are not sons of God. In Romans 8:1-17, those who walk in the Spirit are sons of God in contrast with those who have mind set on the flesh who refuse to submit to God's law. In Romans 3:31, our faith upholds God's law.