Before delving into an answer, it is important to note that the spiral arms of galaxies are not fixed, solid objects; rather, they are patterns of bright stars and gas clouds within the overall form of the galaxy. The space between the spiral arms is not empty, and stars can move in and out of the arms as they orbit through the galaxy.

Ray Carlberg in the astronomy department at the University of Toronto sent in this description of what scientists know about the nature of spiral galaxies:

"The basic physics of why galaxies have spirals is known, but the details remain controversial, sometimes intensely so. Spirals exist only among flattened or 'disk' galaxies. These galaxies are differentially rotating--that is, the time to complete a full rotation increases with distance from the center. Differential rotation causes any disturbance in the disk to wind up into a spiral form. The trouble with this simple explanation is that the differential rotation would cause spiral features to wind up too quickly, so galaxies would not look like spirals for any appreciable length of time.

"The second important piece of physics for understanding spiral structure is that the stars and gas in the disk of the galaxy exert an appreciable gravitational force. That force helps maintain the spiral form against the tendency to wind up. Almost everyone agrees on this basic physics.

"So, why do disk galaxies often have spiral shapes? There is observational evidence that nearby companion galaxies or an asymmetric, bar-shaped concentration of mass can drive a spiral wave in the disk of the galaxy. Disks that lack such forcing features are the tricky ones to explain. One explanation centers on the fact that gravitational systems act to increase their central binding energy. Spiral arms remove angular momentum from the center of the galaxy, allowing it to achieve a state of higher binding energy. There are two main versions of the theory of spiraling: one in which the waves are steady and long-lived, the other in which spirals are transient features that come and go. The natural, but not very easy, test is to observe spiral galaxies for a few hundred million years and see what happens."

Debra M. Elmegreen, Maria Mitchell Professor of Astronomy at Vassar College, and Bruce G. Elmegreen, staff scientist at the IBM T.J. Watson Research Center, have extensively studied this question. Here is their response:

"Most spiral arms in galaxies are density waves, which are compression waves (like sound) that travel through the disk and cause a piling-up of stars and gas at the crest. The wave is temporarily sustained by the force of its own gravity, but it eventually wraps up or gets absorbed at orbital resonances, places where random stellar oscillations have the same period as the local wave.

"In some galaxies, a large central bulge can prevent the wave from reaching a resonance; the wave then reflects off the bulge, giving rise to a giant standing spiral wave with a uniform rotation rate and a lifetime of perhaps 5 to 10 disk rotations (roughly one to two billion years). In all cases, the stars and gas rotate around the galaxy's center faster than the wave in the inner parts of the disk, and slower than the wave in the outer parts. This differential rotation forces gas to enter the wave at a high speed in the inner regions, causing it to shock and form long, thin dust lanes in each spiral arm. Some density-wave galaxies, like M81, have highly symmetric spiral arms; others, like M101, have several arms and less overall symmetry. The difference between these two cases is related to the symmetry of the perturbation that formed the arms in the first place, and to the relative importance of the standing wave pattern, which tends to be symmetric.

"Density waves have many possible origins. A large central bar, such as is seen in NGC 1300, may drive a two-arm density wave for a relatively long time, eventually causing the gas in the outer disk to move outward and wrap into a giant ring at the edge of the galaxy's disk. A companion galaxy can also generate a two-arm spiral by tidal forces. Such tidal arms probably last only for several rotations before they either wrap up and disappear or initiate a longer-lived standing wave. The Whirlpool galaxy, M51, has companion-triggered spirals. Galaxies that appear in visible light to have neither bars nor companions can still have spiral waves. These galaxies may have hidden weak bars or small companions that trigger the spirals, or they may be excited entirely by small asymmetries and perturbations within their disks.

"Some galaxies have no long spiral arms at all, but only numerous, short and non-symmetric arms, as in the Sculptor group galaxy NGC 7793. These arms are probably not density waves at all, but are short-lived star-forming regions that are sheared into spiral-like pieces by differential rotation of the galaxy. Such star-formation features last only as long as the bright, high-mass stars that dominate their light--about a hundred million years, less than a single rotation period of the galaxy. They apparently form when the disk is too stable to sustain a wave, or when there are no perturbations that could drive the formation of spiral arms."

Jerry Sellwood, who studies stellar group dynamics at Rutgers University, provided a helpful, broad overview of this area of research:

"The first part of your question was posed in 1850 by the Irish astronomer Lord Rosse after seeing the strikingly beautiful spiral pattern in Messier 51. While astronomers now generally agree that the spiral patterns in the majority of bright galaxies are density waves, experts still differ on how the arms are formed.

"A density wave is shorthand to describe the way stars in a galaxy are packed a little more closely together in the arms and spread more thinly in between the arms. The density variations travel round the galaxy, much like a sound wave through the air; therefore a spiral arm is not a simply a concentration of co-orbiting stars and gas.

"The problem of the origin of density waves is difficult because the billions of stars in a galaxy all exert gravitational forces on each other. Just as we can understand pressure in a gas without having to calculate the motions of individual molecules, we can treat a galaxy as a massive 'stellar fluid,' but the real difficulty stems from the long-range nature of the gravitational force. Computer simulations develop spirals spontaneously, confirming that gravitational dynamics is the important physical process, but it is hard to understand how this process works even inside the computer.

"Fortunately, nearly every one agrees that spiral patterns extract gravitational energy from the field of a galaxy. The inexorable force of gravity tries to pull the stars in a galaxy closer towards the center. The gravitational force is balanced by the orbital motion of a star (like a stone whirled on a string) which generally prevents it from settling any deeper on average into the galaxy. The spiral arms are a kind of catalyst that brakes the orbital motion of some stars, allowing them to sink slightly closer to the middle. Those with technical training will realize that if some stars lose angular momentum others must gain equally and, in fact, the stars that lose are near the inner end of the arms while those at the outer end gain. The gravitational stresses arising from the spiral density wave provide the torque.

"So, just as in capitalist economics, the stars near the center of a galaxy with little angular momentum have some taken away and given to those further out that were already angular momentum rich. Moreover, this process liberates energy: the stars that settle slightly closer are in the strong field of the inner galaxy, while the galaxy has a weaker hold on those that are pushed out.

"Hence it is energetically favorable for spiral patterns to develop because they provide the only possible torques to enable stars to become more tightly bound to the inner galaxy. Precisely what pulls on what to make the arms develop, the how part of your question, is much harder. There are several competing theories, all of which undoubtedly contain elements of truth, but none has gained wide acceptance.

"In the case of Messier 51, most experts agree that tides raised by the small companion galaxy are probably responsible for some of its exceptionally regular pattern, but too many galaxies display spiral arms for them all to be caused by interactions. Bars at the centers of galaxies are another idea that may drive spirals, but Messier 33 provides a clear counter-example to indicate that bars are not a universal mechanism either. I personally hope that the explanation will eventually be found in a recurrent dynamic instability (a flag flaps in a breeze because of a recurrent instability), but this idea still needs a lot more work.