There are many ways to keep our minds away from satanic influences and thoughts; most of us learned as children to hum our favorite hymns should bad thoughts enter our minds. I have to add that humming your favorite hymn may actually be counter-productive. If you hum a hymn to get rid of bad thoughts, the hymn becomes associated with the bad thoughts and serves as a trigger for the thoughts. This means that the next time the hymn is sung or heard, you might think the inappropriate thoughts. I recommend this tactic be used sparingly with frequent changing of the hymn. Additionally, it is best if the hymn is sung (it can be silently to yourself) because singing the words will help push out bad thoughts more readily than simply humming the melody will. Many of us learned this scripture in seminary about the importance of keeping our thoughts pure: "But this much I can tell you, that if ye do not watch yourselves, and your thoughts, and your words, and your deeds, and observe the commandments of God, and continue in the faith of what ye have heard concerning the coming of our Lord, even unto the end of your lives, ye must perish. And now, O man, remember, and perish not" (Mosiah 4:30).
There are many "how to" guides for keeping our thoughts clean and our minds pure, but knowing the "how" without the "why" is like participating in rituals at church without conversion. For example, it is important for a child to learn that partaking of the sacrament is essential; however, as she grows older she should also learn why we partake of the sacrament and not just that we do. Without a conversion to the "why" of doing something, life-long endurance is nearly impossible.
So why are we commanded to keep our minds pure? Why should we shun vulgarity and pornography? At a basic cognitive level, it is because when we think unclean thoughts, we cannot and are not thinking of spiritual things, since we cannot have two thoughts at once. But that is hardly a sufficient explanation or reason for conversion or salvation. Purity is a mental and spiritual state. It is keeping unholy thoughts out but it is also more than that; the word also implies being free of superfluous substance, just as 24 carat gold is free from other elements. So purity is not just freedom from contaminating objects, but also freedom from all additives, even if they might be good, or at least neutral, things. That is why we cannot be too focused on good things if we leave the weightier matters undone (see Elder Dallin H. Oaks' talk on this topic). Purity is a process; it is becoming cleansed of all imperfections; thus, that which was impure can become pure.