We are accounted righteous before God, only for the merit of our Lord and Saviour Jesus Christ by faith, and not for our own works or deservings. Wherefore that we are justified by faith only is a most wholesome doctrine, and very full of comfort; as more largely is expressed in the Homily of Justification.
XII. Of Good Works
Albeit that good works, which are the fruits of faith and follow after justification, cannot put away our sins and endure the severity of God's judgment, yet are they pleasing and acceptable to God in Christ, and do spring out necessarily of a true and lively faith, insomuch that by them a lively faith may be as evidently known as a tree discerned by the fruit.
XIII. Of Works before Justification
Works done before the grace of Christ and the inspiration of the Holy Spirit, are not pleasant to God, forasmuch as they spring not of faith in Jesus Christ, neither do they make men meet to receive grace, or (as the School authors say) deserve grace of congruity: yea, rather for that they are not done as God hath willed and commanded them to be done, we doubt not but they have the nature of sin.
Here is good doctrine from the Thirty-Nine Articles, explaining the relationship of justification and good works. Allow me simply to clarify a couple terms from Article 13.
First, when the article speaks of the "inspiration" of the Spirit, it is not using the term in the more technical sense which we do nowadays when we talk about the inspiration of the Bible. The article is saying that without the work of the Holy Spirit, no one can do anything pleasing to God.Second, the reference to the "grace of congruity" may be unfamiliar to you. The "school authors" were the theologians of the Middle Ages who attempted to systematize and reconcile biblical teachings and church tradition. They wanted to harmonize faith and reason, and they began to do so in the categories of thought they learned from Aristotle. Rather than producing healthy growth in biblical knowledge and faithfulness, this produced mutations. One of those mutations grew up in the doctrine of justification.
The thought developed that there were two kinds of merit which attached to human works. Good works done with the assistance of grace deserved God's reward as a matter of justice. This was called "condign" merit. Good works done apart from grace, in man's own strength, did not deserve God's reward as a matter of justice. God was not obligated to reward such works. However, because they were good, it was fitting or congruent for God to reward these works. This was called "congruent" merit. Some thought that these good works prepared men to receive God's grace. Articles 12 and 13 deny this teaching, and rightly so. The righteousness of Jesus Christ is the only ground for our justification!