Refinery
A petroleum refinery is a processing plant that converts crude oil into a mix of different finished petroleum products. In its natural state, crude oil is a complex mixture of many different liquid hydrocarbons and has no direct uses other than as a fuel in boiler furnaces. The refining process turns crude into an array of far more valuable products by separating components and converting some components into others. Almost all crude is processed through a refinery.
See here for a global list of refineries: Refineries by region
How it works
A refinery is a large and highly complex processing plant, with many different steps. However, you can think of these as all falling into one of four broad categories.
Separation (distillation) - The most basic and common step is distillation, where crude oil is separated into its component parts or distillation cuts. This is typically done in a two stage process, atmospheric distillation, then vacuum distillation.
Conversion - Next, these distillation cuts are further processed through conversion units to transform them into a more profitable mix of products. A typical refinery has 5-20 different conversion units, such as an FCC or coker. This allows the refinery to produce a mix of petroleum products that is more valuable than the mix that occurs naturally in the crude oil being processed. Many of the outputs from one conversion unit are also then fed into another conversion unit creating a complicated set of feedback loops and interconnections.
Treating - Next, refiners employ treating units such as the hydrotreater units to improve stream qualities and to remove contaminants.
Product blending - Finally, refiners blend different intermediate streams to create batches of finished products meeting the exact quality specifications for the end products that the market wants. Most finished products are a blend of many different intermediate streams of varying qualities that are combined to achieve a final blend quality that exactly matches market requirements.
Crude oil is the primary input to a refinery, but it is common to supplement with small volumes of intermediate streams sourced from other refineries, and some non-crude feedstocks such as butane from gas processing. Typically refiners receive crude oil in very large batches or cargoes, either by pipeline or by ship.
A refinery's capacity is typically measured by the amount of fresh crude oil that it processes through atmospheric distillation at the front end of the plant. However, at typical refinery has many other process units beyond distillation, with each unit designed to perform one or more of the refining steps above for a specific stream. Each process unit itself is a highly complex, capital intensive process. The greater the number of process units a refinery has, the greater its complexity, capital intensity, and (ideally) profitability.
Most refinery process units are continuous processes - that is they have a constant flow of hydrocarbon through and operate essentially 24/7. To maintain this continuous activity refineries rely on large tank farms for storage of crude oil, products, and intermediates.
Every refinery produces array of different refined products, with the exact mix driven by the quality of the crude oil processed and the different conversion process employed. A typical refinery will produces dozens if not hundreds of different refined products and product grades simultaneously. The mix will depend on both the quality of the crude processed and the number an type of different conversion process units the refinery has.
The major refined products (gasoline, diesel, jet fuel and fuel oil) are typically moved out of a refinery in large batches or cargoes by either pipeline or ship. Most refineries also have a truck rack for distributon of gasoline and diesel by truck to retail stations in the local market.
Economics
The profitability of a refinery comes from the difference in value between the crude oil that it processes and the petroleum products that it produces. Most of a refiner's margin comes from the higher-value "light products" (i.e., gasoline, diesel, and jet fuel) that it makes. However, refineries also, unavoidably, produce some lower-value products (such as fuel oil) in the process. Some refineries also generate incremental value from producing some smaller-volume specialty products (such as asphalt, lubricants, waxes, and solvents).
Refinery profitability varies widely over time. Market conditions are constantly changing the price that a refinery see for the crude and other feedstocks that it buys, and the products that it makes. It is largely the change in relative prices (eg the price spreads or differentials) that drives a refinery's margins over time, rather than absolute prices. Put another way, higher prices overall do not necessarily mean higher margins for refiners.
The profitability of one refinery vs another can also vary significantly, even if both see the same market conditions. The design of a refinery largely drives the type of crude that it runs and the mix of products that it makes. This can translate into very different margins for refineries of different designs. Additionally, how well a refinery is operated will affect throughput, yield, and operating costs; all of which contribute to differences between one refinery and another.