Our tradition has long taught that it is a great mitzvah to do the right thing, even if for the wrong reason. “A person should, leolam, always be engaged in Torah and mitzvoth even if sheloh lishma, not for its own sake; as from doing them not for their sake, one will come to do them lishma, for their own sake” (Pesachim 50b).
This teaching, quoted in the name of Rav, the founder of the great Babylonian academy in Sura, appears in numerous places in the Talmud. He understood that it is actions, more than motives, that matter. This is especially true in the case of mitzvoth between man and man, where the purpose is to help others (even if it’s done somewhat grudgingly). It is also true in our relationship to G-d, where one might have thought that intent is more important—G-d, after all, does not need our actions.
As the Sefer Hachinuch noted a thousand years later and in a different context, it is actions that mold character; and acting out mitzvoth will influence our mindset, slowly but surely moving us towards doing mitzvoth for their own sake. In fact, it is next to impossible for a human to be purely motivated. We are a scheming species, and often do not even know ourselves what motivates us. The operative word is leolam, always. There is always an element of shelo lishma in our mitzvoth, but our goal is to limit this over time and by repeated performance of mitzvoth, so that we are moving in the right direction towards the (possibly unattainable) goal of totally acting for the sake of the mitzvah. Our job is to do what is right—not to philosophize about why we act the way we do.
Yet, at the same time, we have a contradictory teaching “that whoever is engaged in Torah for ulterior motives, it would have been better had he not been created”. The true essence of a person is what lies in his heart, what motivates him to act. At times, it may be difficult to translate our true desires to action, but having a pure heart is the most important prerequisite to creating a good person. “G-d desires the heart”, and it is from the heart that actions will flow.
One could argue that we have a fundamental disagreement—is it the heart or the hand that matter most?. Yet our commentaries felt that these two views can and should be reconciled. Tosafot, in a number of places, distinguishes between two types of “ulterior motives”. The first is when one is acting for honour, or for monetary gain and the like. While not ideal, doing mitzvoth—even for personal gain—is most laudatory. On the other hand, one may learn in order to know just enough so he can attack our tradition, or to try to stump and thereby embarrass his teacher. Here, the learning is harmful; and one who engages in such is compounding his sin by using Torah as a means towards that end. That is not why we were created.
The Netziv (Responsa Mesheev Davar #46) offers an alternative—and what can only be described as a frightening—solution. He explains that the determining factor here is whether one is following precedent or innovating. In the former case, one is doing what Jewish law demands—and we cannot fault one for his feelings towards such obligations. I may not want to sit in a sukkah or keep kosher, and I may do so for all kinds of reasons—but I am doing what I must do, and am thus rewarded, regardless of motive. But when one introduces new practices to Judaism that that are not truly required, one must be certain, the Netziv claims, to ensure that one is acting for the sake of heaven.
Innovation, hiddush, in understanding a piece of Gemara or dealing with new social realities can be wonderful and necessary. But the sole motivation for such must be to make Torah greater and even more beautiful. And while man can see what others do, only G-d can know what truly motivates us.
 This “debate” seems to me to somewhat parallel the debate as to whether we should follow the Rambam’s view that the one’s inclusion in the Jewish community is dependent on proper belief. Even those whose actions are full of sin can merit the World to Come if they follow the thirteen principles of faith, whereas a wonderful person who does not accept such loses their share in the next world.