For the purposes of most casual conversational situations such as those you describe, the idiomatic answer is "How did you know?" It confirms the information just revealed and expresses a little surprise that the other person knew that information.
In contrast, and in other situations, "How do you know?" can be a bit of a challenge. It may show that you doubt that the other person know