贵 means expensive and valuable. 你贵姓? means what is your valuable family name? it's a polite way, ( normal way is 你姓什么?)
we usually ask "您贵姓"?to somebody which is in front of you.
if you want to know the family name of somebody else, you can just ask "他姓什么?", but if he is there with you, then you can just ask him"您贵姓?
BTW, in chinese names, the family name is 姓,it's in front of the frist name.
example: 姚明 (姚 is family name, 明 is first name ), family name is as same as last name in English, just different order is all