General Astronomy/Galactic Formation
During the early ages of the universe, the universe was very homogeneous and the only structure present was small fluctuations in the density of the primordial gas cloud. This gas cloud, primarily made up of hydrogen and helium, was formed in the first billion years after the big bang during recombination. Small fluctuations in the primordial gas cloud are observed in the cosmic microwave background, homogeneous to one part in one hundred thousand, which is an indication of the density distribution of the universe after recombination. Areas with slightly higher density had enough gravitational attraction to overcome expansion of the universe and collapsed into the first protogalaxies.
Over time, areas of higher density began to collapse due to gravitational attraction and formed the first protogalaxies. The first protogalaxies were formed when the universe was approximately 1 billion years old. Though these initial protogalaxies are too young to have formed stars, they emit infrared radiation as gas falls inward and heats due to collisions. Protogalaxies are theoretically observable due to this emission, but they are very dim and far away (approximately 12 billion light years) since they are the earliest structure to be formed after the big bang. Due to these difficulties, protogalaxies have never been observed.
As protogalaxies collapse on themselves, they begin to form a distinct core and halo much like mature galaxies. Particles near the edge of the protogalaxy can lose energy through collisions with the surrounding gas and fall into the core. It has been speculated that the infalling matter forms unstable stars of around 100 solar masses that rapidly collapse into black holes that have been observed at the center of mature galaxies . As matter falls into the black hole throughout the life of the galaxy, it releases large amounts of energy which have been observed as quasars and radio galaxies.
Dark matter present in the protogalaxy has a weaker interaction with surrounding particles. The dark matter present in the protogalaxy remains in the halo while ordinary matter falls into the core, since the primary mechanism for bringing matter into the core is collisions with surrounding gas. Dark matter, due to its weak interaction, has very few collisions and remains gravitationally bound in the halo.
Star Formation and Death
As gas continues to fall inward towards the core of a protogalaxy, its density continues to increase and dense clouds begin to form. These clouds begin to orbit the center of mass of the protogalaxy due to conservation of angular momentum. When two gas clouds collide, they form an area of higher density at the collision front. These high density regions become gravitationally unstable and collapse into balls of plasma, forming protostars. As material continues to rain in on the protostar, deuterium fusion begins and a star is born. As these stars begin to burn and emit light, they dissipate the surrounding cloud of gas due to solar wind and leave a star cluster. These initial areas of star formation may be the source of globular clusters, some of which have stars which are nearly as old as the age of the universe.
These newly formed stars form the first heavy elements of the new galaxy in their cores as they burn their hydrogen supply. Hydrogen is converted to heavier elements through nuclear fusion until the star has formed a critical mass of iron. Iron is very stable and heavier elements do not release heat from fusion, and the star begins to exhaust its fuel supply. Without a heat source, the star undergoes a catastrophic collapse under its own weight, which ends in a supernova.
As they reach the end of their life, the most massive of these stars explode in supernovae, the explosion from which may outshine the rest of its parent galaxy. As the star explodes, it forms heavy elements (heavier than oxygen) by the process of nuclear fusion. These heavy elements are ejected from the exploding star into the galactic gas cloud. Supernovae are responsible for heavy element formation and distribution through the newly formed galaxy, which previously only contained hydrogen and helium formed during recombination.
Elliptical vs. Spiral Galaxies
A new galaxy will most likely form into either an elliptical or spiral galaxy, though other types of galaxies are present (such as a ring or lenticular galaxy.) The type of galaxy formed depends on the initial rate of star production. If the new galaxy forms stars slowly, then the gas cloud has enough time to exchange energy through collisions and the cloud elongates into a flat disk-shaped spiral as matter falls in to the plane of rotation. If the galaxy forms stars quickly, it can use up its supply of gas before sufficient energy has been exchanged to form a disk. These galaxies resemble their initial elliptical shape before star formation. Other theories speculate that elliptical galaxies are formed from collisions of other spiral galaxies, which distort into the elliptical shape after collision. 
Due to their high rate of star production, elliptical galaxies quickly exhaust their gas supply and no longer have active star formation. As such, elliptical galaxies mainly consist of old Population II stars with large numbers of globular clusters. Elliptical galaxies also do not show the characteristic ordered rotation of spiral galaxies since they did not have time to exchange sufficient energy during their formation.
Spiral galaxies, which form stars more slowly, still have ongoing star formation today and as such their star population is much younger. Since spiral galaxies take a long time to form, they exchange considerable energy over their lifetime which collapses matter into a disk with high angular momentum, resulting in the characteristic rotating disk.
Large Scale Interactions
Much like gas clouds in protogalaxies that can interact to form new star clusters, entire galaxies can collide to form new galaxies. As galaxies pass near each other, they can begin to orbit. If the galaxies pass close enough to each other, tidal interactions will cause their orbits to decay and the galaxies consume each other. The shape of the galaxies may be highly distorted during the collision and star formation increases, powering starburst galaxies which are among the most luminous galaxies. This period of increased star formation can last for ten million years or more and the rate of star formation can be ten to one hundred times that of a typical galaxy . The black hole at the center of the galaxy will also show increased emission as material from the collision falls into the galaxy's core.
If galaxies pass near each other but do not fully collide, they can eject large quantities of matter from gravitational interaction, sending gas clouds and lone stars into intergalactic space. These near-collisions also distort the galaxies involved and is one possible mechanism for the formation of lenticular galaxies.
Though large scale interactions were previously thought to be uncommon, the Milky Way was found to have several smaller galaxies orbiting it which suggests that the process is more common. The Sagittarius Dwarf Elliptical Galaxy (or SagDEG) is a small dwarf galaxy orbiting the core of the Milky Way at a radius of about 50,000 light years. The SagDEG is currently passing through the disc of the Milky Way and has been greatly distorted due to gravitational interaction. The small and large Magellanic clouds are also dwarf galaxies which will eventually merge with the Milky Way.