On the images captured by Webb telescope one can see some lights with 6 rays, but most others don't have any. One would expect the optics to transform all light sources at infinity in the same manner. What causes these differences?
These are diffraction spikes. They are an interference pattern caused by the the arms and shape of the telescope.
They occur around whatever is bright enough in the image which in this case is all the stars that are within the milky way. These stars show up super bright because the Webb is trying to look for super dim objects in the deep field.
The spikes very near the star (horizontal and two diagonal ones) are the three arms and the inverted image of those 3 arms.
You can think of them as a type of "shadow" the telescope is casting.
The larger lines which form the 6 points are caused by the non circular shape of Webb also causing an interference pattern.
The geometry of the arms was chosen so that the diagonal spikes from the arms line up with the spikes from the shape of the telescope. This was done to minimize the effect.
It is worth noting that all the galaxies also have diffraction spikes but they are much dimmer due to the amount of light and diffuse due to the non point like nature of the extended objects. In particular the interference pattern is a type of Point Spread Function. As Prof Rob explains this is a type of Fourier Transform done on the light due to the missing modes from the shape and arms of the telescope. A more spread out object exhibts these Point Spread Functions much less because the missing information is recovered from a slightly different angle.
Any image is the convolution of the intrinsic image of an object and the point spread function of the telescope, which is (in broad terms) the Fourier Transform of its aperture and any obscuring supporting structure for the secondary mirror.
Even the nearest stars appear as point sources to JWST, so their images are essentially just the Fourier Transform of the aperture/telescope. The tiled, hexagonal structure and secondary mirror support of JWST means that this looks like a 6-pointed object, with the brightness of these "diffraction spikes" decreasing rapidly with distance from the true position of the source. The main 6-pointed pattern is due to the hexagonal shape of the primary mirror, combined with the three-strut secondary support. There is also a horizontal spikelet that is just due to the mirror support (e.g., see here).
In contrast, most galaxies are not point sources, they are resolved into images of finite size by JWST. These are also convolved with the 6-spike point spread function, but the finite size of the sources blurs it out so it can't be obviously seen in the final image. i.e. The spikes contain some small fraction of the brightness and are very narrow. In a galaxy image, this small fraction is spread across many pixels and becomes much less obvious.
In pictures, another factor in how prominent the diffraction spikes appear (around stars) will depend to some extent on how brightness in the image is scaled with respect to true brightness. Often the images are scaled to enhance the appearance of fainter stars/galaxies and this makes the diffraction spikes more prominent, especially around the brightest stars in an image.
Every source of light has their spikes in the image. Even extended galaxies have broad and washy spikes.
Why are they not always visible then?
The only difference is the surface brightness of the sources. The point spread function of the telescope aperture spreads the source brightness always over the same pattern, regardless of its brightness. This point spread function has a gigantic main lobe that contains almost all of the intensity and the spike intensity is lower by many orders of magnitude. Therefore, only bright sources gather enough detector counts in the spikes to make them discernible over the surrounding intensity.
For less bright sources, the spike intensity is just so low, that we cannot see them.
As tobalt wrote, it's all about surface brightness. But the crucial bit, which is not apparent enough, is just how much brighter these stars appear than the further away galaxies. If JWST had taken the picture with lower exposure it could have captured this more faithfully, perhaps like this:
...but that wouldn't be very useful for scientific purposes and certainly not look as inspiring.
Due to a combination of overexposure of the sensor itself, and post-processing, we see much more information in the actual published image – but at the price of discarding the direct information about the foreground stars' brightness, and also of blowing the intensity of their diffraction spikes completely out of proportion. (Which does of course provide a way of estimating the true brightness again, as well as a visual impression of their "brilliance".)
On the images captured by Webb telescope one can see some lights with 6 rays, but most others don't have any
They have, simply bigger blobs of light (galaxies, etc) outshines diffraction pattern. Take a look at some section of pic
which was zoomed-in, then edge detection algorithm was performed and white balance was automatically corrected.
Every object in this photo is displaying "spikes." The question begins with the false premise that "some...have rays and others don't."
Some rays are bright, while some rays are dim and diffused. The intensity of the rays depend on the brightness of the light source. They also depend upon the relative diameter in the photo of the light source. Larger sources such as galaxies have diffused rays, seeming to not display rays to the naked eye observer.
Closer examination of the photo show every light source to be producing the rays.
I would like to add a few things to the nice answer by @profrob, because I recently had a similar question:
The "cross" pattern is due to light reflecting off other surfaces in one of two ways. Most likely it's either an artifact of your camera imaging system where reflections or diffraction inside the camera from bright sources are visible in darker fields (https://petapixel.com/2018/05/19/the-physics-behind-sunbursts-and-how-it-can-help-you-focus-your-photos/), ...or it's due to scratches/scoring on a window you're looking through (Why do lights appear like straight lines on a windshield of a car? (becomes more prominent at sunset and night))
So in addition to the other answers, the "spikes" can be caused by:
reflections or diffraction inside the camera from brighter sources are visible in darker areas
scratches/scoring on the glass that light propagates through