The word "why" is usually used in reference to a question or a reason. However, in this case it is used as an interjection that expresses pleasant feelings about something someone has said or done. Saying, "Why, thank you!" is the same as saying, "I am pleased with what you said or did. Thank you for it!" Often the phrase is used when someone has done or said something good that was unexpected.