The following is stolen (borrowed, I'd like to believe) from the Star Trek BBS:
Quoting Howard A. Anderson, ASC from "Out-of-this-world Special Effects for 'Star Trek'" in the October 1967 American Cinematographer:
One of our most difficult assignments for the series was to create the impression that the Enterprise was racing through space at an incredible speed--faster than the speed of light, which is 6,000,000,000,000 miles a year.
Other space shows have shown spacecraft more or less "drifting" through space. We wanted to avoid that cliche. The solution did not come easily or quickly. We experimented with dozens of ideas before we hit on an effective solution.
The principal elements in our solution are a space sky and the use of an Oxberry optical printer. To make the space sky, we painted black stars on a white background about 2 1/2 feet by 3 feet, arriving at a suitable design. We then made a series of black-out mattes that we could use later with the sky in the optical printer.
The advanced Oxberry printer was unique at the time. It is capable of making a 5-to-1 reduction through a 4-to-1 enlargement with continued automatic focusing. The space sky was photographed and a still frame used on the optical printer. We tracked the space sky to the left, to the right, to the top and to the bottom, using a different black-out matte on each pass and superimposed these various moves at different speeds. We were able to create the illusion that the Enterprise was racing through space at an incredible speed. We start with a space sky filled with some 500 stars and finish with perhaps 30 on each pass. In the automatic focusing process, we got from a 3-foot scope down to about 10 inches.
Although that's just how Anderson did it. ST's effects were produced by multiple different effects houses. From the same article, Linwood Dunn discusses how his company Film Effects of Hollywood created their starfield effects:
The background star fields were created by punching holes of various sizes, in proper scale and location, in large sheets of black paper, backed up by special diffusion screens and color filters to create the desired effect. By combining scenes made at varying camera distances and travel speeds, a realistic illusion of depth was created. This was particularly important in star fields which tied-in with forward or reverse travel of the ship, and seen on its forward viewing screen.
So basically they both photographed multiple starscape layers which were moving at different speeds relative to the camera. Superimposing them created the effect of 3-D movement, nearer stars moving faster than more distant ones.