In the original law that Moses received from God on Mount Sinai, the fourth commandment said,
Remember the Sabbath day by keeping it holy. Six days you shall labor and do all your work, but the seventh day is a Sabbath to the LORD your God. On it you shall not do any work...(Ex. 20:8-10)
Interpretation of exactly what was meant by the instruction to do no work on the Sabbath was the critical question; the answer to that question changed and evolved over time.
In Deuteronomy, the people were instructed to leave some grain in the fields to be harvested by the poor, the widows, and others as they had need (Deut. 24:19) This law is repeated in Leviticus 19:9, and is the basis of Ruth's ability to gather grain from the fields owned by Boaz (Ruth 2:2) On the basis of this practice, the disciples were not stealing grain from the owners of the fields; whether or not they were doing the work of harvesting on the Sabbath would have been debatable.
By the time Jesus lived, however, the Pharisees had arisen to a position of great influence in the culture of the area. According to Jesus, the Pharisees used a very limited interpretation of the law and demanded very strict obedience of it by others, while not adhering to the same practices themselves. Jesus pointed to the way in which David broke the religious laws by eating the bread which had been given in offering to God in the temple, implying that David's actions were not sinful. Jesus also pointed out "on the Sabbath the priests in the temple desecrate the day and yet are innocent" (Matt. 12:5).
Jesus' point was that His disciples were with "one greater than the temple" (Matt. 12:6), referring to Himself. Because the disciples were acting under the mercy and authority of the "Son of Man" Who was "Lord of the Sabbath" (Matt. 12:8), they were innocent of any violation of the Law of Moses or of tradition.