Why do aircraft (namely military) need/use formation lights?
Formation lights are used so that aircraft can fly close formation at night.  They are typically luminescent strips positioned so that an aircraft can be in the desired position (i.e. fingertip formation) and have references fore-aft, up-down, and near-far similar to references as used for daytime formation flying.  Something along the lines of, "keep the position light just below the formation light (strip) under the cockpit and the aft fuselage light (strip) just touching the wing hardpoint, and you're in the right position.  If the position light goes farther below the strip, you're getting high; if the hardpoint is sliding back into the aft light you're getting close, etc."

Obviously these references are very specific to each individual aircraft.

You can see the formation lights in this picture of an F-14; the dots are actual position lights, while the strips are the formation lights.  Source


As to why only military aircraft have them... how often have you seen or heard of civil aircraft flying formation at night?  Yeah, me neither.  There are very, very few non-military reasons for formation flight, and essentially none that require it to be done at night.