The passage you are asking about—Matthew 5: 27-28—is a part of Jesus' Sermon on the Mount, and one in a series of examples he uses to emphasize the new dimension of spiritual awareness he is teaching and demonstrating:
"You have heard it said 'You shall not commit adultery.' But I say to you that every one who looks at a woman lustfully has already committed adultery with her in his heart."
In this and other passages with the same structure ("You have heard it said ... but I say to you ..."), Jesus seeks to move us from simple, fear-based obedience to the law upward to a higher dimension of spiritual awareness. This higher dimension does not abolish the law, but rather fulfils it (Matthew 5:17) by allowing us to understand the spiritual Truth of which the law is a temporal expression.
At this higher level of spiritual awareness, the power lies not only in what we do, but in what we think and believe. Our thoughts have creative power; and if we linger lustfully on adulterous thoughts, we are creating a negative, adulterous energy in our lives just as if we had acted on those thoughts. The act of adultery is an expression of thoughts of lack and limitation. We want, and try to claim, something that is not ours in consciousness because we have allowed another person to represent the love we lack and are seeking. Even if we don't sexually commit adultery, those fear-based, separation-based negative thoughts are taking root in our consciousness and impacting our lives. At earlier levels of spiritual awareness we might not grasp the power of our thoughts, so the law was lovingly designed to at least prevent us from acting on negative thoughts. Now, however, Jesus is calling us to recognize where the real power lies, and to realize that it is only by changing our thoughts that we can release fear, embrace the true Source of love, and allow that loving energy to create a new and loving life experience.