To add to Hornet's response, a large part of effect comes from reflections inside the lenses. Telephoto lenses are more prone to the effect because of more distance between each lens elements. Think about it this way. A lens with 3 elements (let's call them 1, 2, and 3). Each has a front (f) and back (b) surface. Light enters 1f. Some light gets reflected off of 2f to 1b. That reflection then reenters 2f to 3f. Some of 3f light gets reflected back to 2b (and even 1f when the light source is strong) and back through element 3 to the focal plane (film plane) of the camera. With greater distance between the lens elements, the light spreads with the spread amplified by the curvature of each element. Hence the lens flare has many artifacts more noticeable with brighter light. (The math gets more complicated if you consider the internal surfaces of lenses. This is what causes light loss in the lens from one end to the other. And here the imperfections of the glass comes into play.)
The shape of the flares tends to be hexagonal since the plates that control the f-stop aperture create a 6-sided hole. The light refracts (bends) around those edges.
The star effect comes from small aperture settings (openings) and the light waves refracting around the 'corners' of the hexagon vertices.
Reflections can be reduced with the use of lens hoods and such as long as they don't get into the field of view.
The physics of light waves as they refract and reflect creates all types of artifacts and effects. I teach a training session on radio control for lifting cranes in industrial environments. One big aspect covers what happens to radio waves as they bounce around and move in the environment. Similar thing happens with light (and water) waves. I use the following as my visual aid.
You probably see the obvious movement of the waves originating from the bottom of the image. You can see the waves reflect off the ship and the waves bend (refract) around the ship. Note the reflections curve following the rules about angle of incidence = angle of reflection. Refraction can be thought of (on a simplistic level) as friction of the surface which slows the waves down. For radio waves the calm areas are dead zones (little to no signal). For light waves, those dead zones represent shadows.
Likely I am providing more than you really want (or need) to know. But that's the professional trainer in me coming out.