It is common etiquette to send a thank you note after an interview. At that point you still hope to get a call back about the position. But if you have already received notification that the position has been filled by someone else, you may still send a thank you email to the hiring manager.
Many business communications are done by email, and in fact, many hiring managers prefer to conduct their hiring process online. According to a 2010 Upwork survey of 500 employers, 64 percent of them preferred that job applications were sent online over the traditional method of applying in person. Sending a thank you for the interview via email even after you learn the position is filled is an acceptable form of communication with the hiring manager.
Even though the hiring manager has not selected you for the position, it is still valuable to send a thank you. For one, it is a polite gesture and shows that you are a mature individual. Secondly, there is always a chance that things will not work out with the person the company hired. If so, sending this thank you could increase the chance of getting a call back as a second choice or to take another position at the company in the future. Sending a thank you note via email only takes a few minutes and keeps you present in the hiring manager's mind.
In your thank you email to the hiring manager you should simply express your gratitude for the opportunity. Thank the hiring manager for considering your application and interviewing you if it reached that stage. Keep the message brief at no more than a paragraph long.
When you send your thank you email to the hiring manager ensure that it is just as professional as the cover letter and resume you sent. Remove any flashy or informal email signatures. Keep the language positive and limited to a basic note of thanks — don't mention your belief that the hiring manager made the wrong decision.