This same question popped up at the Hermeneutics StackExchange, here:
Are good works supposed to be seen or not?
My answer there is pasted below.
While these two statements may seem self-contradictory, there is a fine line which differentiates them.
TL;DR: Matthew 5:16 says you should not ever be ashamed to do God's work in public. However, Matthew 6:1 warns that you should also not do these works in public simply for the sake of public attention.
The commandment for us to shine our light is given so that we will not allow ourselves to fall into contentment, and simply keep our faith to ourselves. Jesus wants the world to know of God's glory, and that cannot be done without ourselves committing to a certain amount of exposure. We shouldn't just simply huddle in our homes and our churches, communing with one another. Instead, we should also be out in the world and spreading God's word and his love. Some of this is done through deeds, which should always be used as a reflection of God's glory.
However, there is a certain amount of caution that should be exercised when we do this. We need to be careful to ensure that the works we do in His name are done only in that spirit. We should not be performing works simply to boast of our own goodness and godliness. This is the admonition given in Matthew 6:1.
To better understand this, these verses need to be put a bit more fully into context. Let's start with Matthew 5:14-16.
14 “You are the light of the world. A town built on a hill cannot be hidden. 15 Neither do people light a lamp and put it under a bowl. Instead they put it on its stand, and it gives light to everyone in the house. 16 In the same way, let your light shine before others, that they may see your good deeds and glorify your Father in heaven.
Here, we are told that we are like a lantern. We are not meant to be hidden or reclusive, but instead allow God's glory to shine out to the world through us. This bit is fairly simple on its own.
Now, for Matthew 6:1-4.
1 “Be careful not to practice your righteousness in front of others to be seen by them. If you do, you will have no reward from your Father in heaven.
2 “So when you give to the needy, do not announce it with trumpets, as the hypocrites do in the synagogues and on the streets, to be honored by others. Truly I tell you, they have received their reward in full. 3 But when you give to the needy, do not let your left hand know what your right hand is doing, 4 so that your giving may be in secret. Then your Father, who sees what is done in secret, will reward you.
This is the slightly tricky bit. Jesus is not telling us at all to hide our light. Rather, he tells us that we should not shine it for the sole sake of drawing attention to ourselves. Our good deeds, while often done in public before the world, are not always for the whole world to see and hear about. Every good deed has its beneficiaries, and its audience should not be more than those who will truly see benefit from it. Anything beyond that is excessive, and is generally done in the spirit of drawing attention more to oneself than to God.
To further clarify this point, some examples of hypocritical and pagan behavior to avoid are given in Matthew 6 - one of which is already stated above:
- Drawing attention to your good deeds in public, "with trumpets ... in the synagogues and the streets ... to be honored by others". (Matt. 6:2)
- Praying for the sake of being seen praying "standing in the synagogues and on the street corners ... babbling". (Matt. 6:5, 6:7)
- Making your fasting obvious, "look[ing] somber ... disfigur[ing your] faces". (Matt. 6:16)
Again, the point here is not that you should abstain from good deeds, praying, or fasting in public. Instead, you should simply do these things without boasting of them or drawing unnecessary attention.
NOTE: All scripture references are copied from BibleGateway's NIV.