In many images of light emitting objects we see such rays. Why do they appear ? What is the math behind their number and direction?

In many images of light emitting objects we see such rays. Why do they appear ? What is the math behind their number and direction?

Those are artifacts of having obstructions in the optics. Ideally, we think of the intensity being recorded as the (squared magnitude of the) Fourier transform of the wavefront passing through the aperture. That is, whenever a wavefront is brought into focus, it undergoes a Fourier transform (in the Fraunhofer limit).
This transform is affected by the aperture, which excludes any part of the wavefront outside a certain range. Indeed for a circular aperture, the Fourier transform of a uniform light source is the familiar Airy pattern. Convolving an ideal image (made with an infinite aperture) with the Airy function results in the familiar bloom seen in photographs of bright light sources.
Many camera apertures, however, use polygons for ease of manufacturing, and so bright sources are convolved with a more complicated function, which can result in your rays. You should take a look at this paper, which discusses simulating these effects and more, since the prevalence of film and photographs in the modern world makes these artifacts almost necessary to lend realism to an image, despite the fact that they are rarely seen with our own eyes. Figure 3 in particular shows a camera aperture, and Figure 5 shows the relation to the Fourier transform.
In summary: the number of spikes is the number of edges in the aperture (iris), and the orientation is just the orientation of the camera.
Other obstructions within the aperture can cause similar effects. For telescopes with a secondary mirror or other installment in the optical path (a very common design), the object and its support struts have their Fourier transforms affect the image. The thin struts in particular case noticeable artifacts, even in Hubble images, as can be seen in the wiki on diffraction spikes.
It isn't Lens Flare, this effect is simply a result of reflections due to the scratches or oil traces left in your camera. A series of parallel scratch lines will reflect light perpendicular to themselves. You can even change the direction of the ray!! You can do it by wiping your Cam's lens in a certain direction using your oily fingers. If you wipe it in one direction, the ray will be oriented perpendicular to it. If you still love your cam's lens so much, you may like to take a look at this post on my blog:
http://emitabsorb.wordpress.com/2012/03/05/childhood-question-finally-solved/
I've analyzed this effect and I demonstrated in the videos how I changed the direction of the rays. You can also easily see this effect at night with your bare eyes, because there are already many scratches on your eye lens.
You can try wiping your camera's lens with your fingers(the wiping should be in one direction only, otherwise no line will occurs) before taking a picture, then if the first two rays that existed before vanished and replaced by a new line then it definitely means that my explanation is correct. But if those two lines still exist, maybe it is due to the scratches on your camera's lens.
Probably, there's no math. It's known as Lens flare. It happens when light reception takes place through lenses (both cameras and even your eyes) in the presence of a bright source which could be either inside or outside the frame. It is a condition that often occurs in photographs when light goes where it is not supposed to go. When light that is not contributing to the image at hand, enters the camera and hits the sensor or film, you get a flare in the photograph. These can wash out the color in the rest of the image, and show up as a polygonal object in the image. The number of rays and their direction depends on the angle of incidence. This link gives useful information regarding Lens flares.
Lens flares could be prevented using lens hoods or a lens having a good anti-reflective coating on it...