The saying dates back to the early 1900s and means to stop talking or making a noise. Presumably the idea behind it was that a sock would form a handy gag to put in the offending person's mouth.
It dates back to the time when wind-up gramaphones (invented in 1887) used a horn to amplify the sound from the needle on the record. The common way to control or limit the volume was to put a sock in the horn, thus muting the sound.