I suppose that you are speaking of the European mute swan. You will notice that they have black knobs on their bills. In the male the knob is larger than in the female, and the male swan is also slightly larger than the female swan, so it is possible for a human with a little bit of practice to tell them apart. It probably is even easier for swans to tell each other apart; not only are they born to it, but they get more practice!
Males and females also have different behaviour, so swans can tell each other by their behaviour. This is not surprising because even human beings can usually tell whether they are watching a man or a woman, even if they are dressed the same.
Some kinds of birds have males and females that looks so alike that they often do make mistakes and have to rely on behaviour. For example in some kinds of penguins, a male might begin to court another male, thinking that he is a female; this does not last; the other male seems to be insulted, and immediately attacks the courting male.