In the temptation story, Jesus is quoting a scripture passage, introduced by the words "It is written." The focus at that point is Jesus acknowledging the truth and authority of God's word. He is saying in effect, God has spoken and I must submit to that word.
In the Sermon on the Mount, the focus is different. Jesus here is a rabbi teaching his disciples (Matthew 5.2), and the contrast in chapter 5 is between his teaching and the teaching of other Jewish authorities, such as the Pharisees or other rabbis. So when Jesus says "You have heard that it was said", this is not a reference to the OT scripture at all. It's a reference to interpretations of the scriptures.
For example, what does it mean that "You shall not murder"? Jesus' answer in Matthew 5.21-22 is that other teachers have limited the scope of the commandment to the physical act of killing someone. But Jesus' teaching is that the command is broken if and when I get angry with someone. This is presumably because the inward attitude is what leads to the outward action. The focus on the inner heart and spirit is a regular theme of Jesus' teaching.
This distinction between OT command and Jewish interpretations of that command is especially clear in Matthew 5.43-44. Here Jesus quotes teachers who say "Love your neighbour and hate your enemy." In fact this is not a command found in the OT. The only command that is found is to "love your neighbour", so "hate your enemy" is an addition from the earlier Jewish teachers. But in Jesus' view to hate your enemy is to contradict the meaning of the command. For Jesus my "neighbour" is anyone I come in contact with and to whom I can show love. The classic picture of this is the parable of the Good Samaritan.
Answer from Peter Kirkpatrick on Stack Exchange