Landwirt is a modern higher word. It implies managing and maintaining a farm as a whole economic structure like a company. You will find it rather in professional job descriptions and "official" written language.
Bauer is an ancient term which is more broad and sometimes also used as swearword. However there are many farmers who are proud for being explicit a "simple" Bauer since they see it from a traditional way. Nowadays a Bauer has many obligations because of rules and economic pressure.
Conclusion: Both words are still in use, it little depends from context if your opposite sees Bauer with negative implications. To be sure and if you want to sound professional I recommend to use Landwirt.