What to do when a thought comes up while meditating
It is important to understand what is meant by 'right view' (samma ditti) which covers the origination of thoughts, the causal arising of thoughts, and the non-self nature of thoughts and the entire thinking apparatus.  

This understanding will help one to be mindful of the arising and passing away of thoughts during meditation without identifying with them. The default view that a thought is mine, created by myself naturally causes one to pursue them. While the right view of the true nature of thoughts eventually leads one to be unbounded from thinking.