The examples and perspective in this article may not represent a worldwide view of the subject. (November 2021)
A building code (also building control or building regulations) is a set of rules that specify the standards for constructed objects such as buildings and non-building structures. Buildings must conform to the code to obtain planning permission, usually from a local council. The main purpose of building codes is to protect public health, safety and general welfare as they relate to the construction and occupancy of buildings and structures — for example, the building codes in many countries require engineers to consider the effects of soil liquefaction in the design of new buildings.  The building code becomes law of a particular jurisdiction when formally enacted by the appropriate governmental or private authority. 
Building codes are generally intended to be applied by architects, engineers, interior designers, constructors and regulators but are also used for various purposes by safety inspectors, environmental scientists, real estate developers, subcontractors, manufacturers of building products and materials, insurance companies, facility managers, tenants, and others. Codes regulate the design and construction of structures where adopted into law.
Examples of building codes began in ancient times.  In the USA the main codes are the International Building Code or International Residential Code [IBC/IRC], electrical codes and plumbing, mechanical codes. Fifty states and the District of Columbia have adopted the I-Codes at the state or jurisdictional level.  In Canada, national model codes are published by the National Research Council of Canada.  In the United Kingdom, compliance with Building Regulations is monitored by building control bodies, either Approved Inspectors or Local Authority Building Control departments. Building Control regularisation charges apply in case work is undertaken which should have had been inspected at the time of the work if this was not done. 
The practice of developing, approving, and enforcing building codes varies considerably among nations. In some countries building codes are developed by the government agencies or quasi-governmental standards organizations and then enforced across the country by the central government. Such codes are known as the national building codes (in a sense they enjoy a mandatory nationwide application).
In other countries, where the power of regulating construction and fire safety is vested in local authorities, a system of model building codes is used. Model building codes have no legal status unless adopted or adapted by an authority having jurisdiction. The developers of model codes urge public authorities to reference model codes in their laws, ordinances, regulations, and administrative orders. When referenced in any of these legal instruments, a particular model code becomes law. This practice is known as 'adoption by reference'. When an adopting authority decides to delete, add, or revise any portions of the model code adopted, it is usually required by the model code developer to follow a formal adoption procedure in which those modifications can be documented for legal purposes.
There are instances when some local jurisdictions choose to develop their own building codes. At some point in time all major cities in the United States had their own building codes. However, due to ever increasing complexity and cost of developing building regulations, virtually all municipalities in the country have chosen to adopt model codes instead. For example, in 2008 New York City abandoned its proprietary 1968 New York City Building Code in favor of a customized version of the International Building Code.  The City of Chicago remains the only municipality in America that continues to use a building code the city developed on its own as part of the Municipal Code of Chicago.
In Europe, the Eurocode: Basis of structural design, is a pan-European building code that has superseded the older national building codes. Each country now has National Annexes to localize the contents of the Eurocodes.
Similarly, in India, each municipality and urban development authority has its own building code, which is mandatory for all construction within their jurisdiction. All these local building codes are variants of a National Building Code,  which serves as model code proving guidelines for regulating building construction activity.
In the Chinese book of rites it mentions that ancestral temple's and house's should be a certain standard length in ancient China they measured land in the chu or well field system so it was important to be precise though most of the actual lengths are lost or obscure.  
In ancient Japan a certain official destroyed a courtiers house because the size was above his rank. 
In Paris, under the reconstruction of much of the city under the Second Empire (1852–70), great blocks of apartments were erected  and the height of buildings was limited by law to five or six stories at most.
After the Great Fire of London in 1666, which had been able to spread so rapidly through the densely built timber housing of the city, the Rebuilding of London Act was passed in the same year as the first significant building regulation.  Drawn up by Sir Matthew Hale, the Act regulated the rebuilding of the city, required housing to have some fire resistance capacity and authorised the City of London Corporation to reopen and widen roads.  The Laws of the Indies were passed in the 1680s by the Spanish Crown to regulate the urban planning for colonies throughout Spain's worldwide imperial possessions.
The first systematic national building standard was established with the London Building Act of 1844. Among the provisions, builders were required to give the district surveyor two days' notice before building, regulations regarding the thickness of walls, height of rooms, the materials used in repairs, the dividing of existing buildings and the placing and design of chimneys, fireplaces and drains were to be enforced and streets had to be built to minimum requirements. 
The Metropolitan Buildings Office was formed to regulate the construction and use of buildings throughout London. Surveyors were empowered to enforce building regulations, which sought to improve the standard of houses and business premises, and to regulate activities that might threaten public health. In 1855 the assets, powers and responsibilities of the office passed to the Metropolitan Board of Works.
The City of Baltimore passed its first building code in 1891.  The Great Baltimore Fire occurred in February 1904. Subsequent changes were made that matched other cities.  In 1904, a Handbook of the Baltimore City Building Laws was published. It served as the building code for four years. Very soon, a formal building code was drafted and eventually adopted in 1908.
The structural failure of the tank that caused the Great Molasses Flood of 1919 prompted the Boston Building Department to require engineering and architectural calculations be filed and signed. U.S. cities and states soon began requiring sign-off by registered professional engineers for the plans of major buildings. 
This section needs additional citations for verification. (October 2016)
The purpose of building codes is to provide minimum standards for safety, health, and general welfare including structural integrity, mechanical integrity (including sanitation, water supply, light, and ventilation), means of egress, fire prevention and control, and energy conservation.   Building codes generally include:
Building codes are generally separate from zoning ordinances, but exterior restrictions (such as setbacks) may fall into either category.
Designers use building code standards out of substantial reference books during design. Building departments review plans submitted to them before construction, issue permits [or not] and inspectors verify compliance to these standards at the site during construction.
There are often additional codes or sections of the same building code that have more specific requirements that apply to dwellings or places of business and special construction objects such as canopies, signs, pedestrian walkways, parking lots, and radio and television antennas.
The energy codes of the United States are adopted at the state and municipal levels and are based on the International Energy Conservation Code (IECC). Previously, they were based on the Model Energy Code (MEC).
As of March 2017, the following residential codes have been partially or fully adopted by states: 
As of September 2005, the following residential energy codes had been partially or fully adopted by states: 
As of January 2004, the following residential energy codes had been partially or fully adopted by states: 
As of Fall 2000, the following residential energy codes had been partially or fully adopted by states: 
As of 1998, three states (Ohio, Michigan, and Virginia) had adopted the 1993 MEC residential energy code. The remaining states had adopted either: a state-written code; a regional code; a prior version of the MEC or American Society of Heating, Refrigerating and Air Conditioning Engineers standards; or no code at all.