What is the same-origin policy?

In short
The same-origin policy is a browser security rule. It stops a script loaded from one website from freely reading data from a different website. Two URLs share an origin only when their protocol, host and port all match.
What an origin is
An origin is the combination of three parts of a web address: the protocol (such as http or https), the host (the domain name, such as store.company.com) and the port (the number after a colon, if one is given). If all three match for two URLs, they have the same origin. The path, such as /dir/page.html, does not count.
Take the address http://store.company.com/dir/page.html as a reference. A page at http://store.company.com/dir2/other.html has the same origin, because only the path differs. A page at https://store.company.com/page.html does not, because the protocol differs. A page at http://news.company.com/dir/page.html does not, because the host differs. The port also matters: http uses port 80 by default, so a page on port 81 is a different origin.
How the policy works
The same-origin policy is a rule that browsers enforce. It controls how a script from one origin may interact with content from another origin. Its main purpose is to protect a user's data when they visit several sites at once. Without it, a script on a harmful site could read your mail or bank page while you are logged in to it in another tab.
The rule is not one simple wall. Browsers sort cross-origin interactions into groups. Writes, such as following a link or submitting a form, are usually allowed. Embedding, such as showing an image or running a script from another site, is usually allowed too. Reading the response is the part that is normally blocked. A script on one origin generally cannot read the content of a response from another origin.
Storage follows the same idea. Web Storage and IndexedDB are separated by origin, so JavaScript in one origin cannot read or write the storage of another. Cookies use their own, looser definition of origins, so they do not follow exactly the same rules.
A simple example
Imagine a page at https://shop.example.com that wants to load data from https://api.example.org. A script on the shop page calling fetch() to the API is a cross-origin read. The browser will normally block the script from reading the response, because the two addresses differ in host.
If the API server wants to allow this, it can send CORS headers. CORS, short for Cross-Origin Resource Sharing, is a part of HTTP that lets a server name the other origins that may read its responses. The browser checks those headers and then permits the read.
Common mistakes
A frequent surprise is that developers expect the policy to block everything across sites. In fact, links, form posts and images from other sites still work. The block is mainly about reading responses with scripts.
Another mistake is to forget the port. Two sites on the same host but different ports are different origins. Also, a file opened from disk with the file:/// scheme is usually treated as its own opaque origin in modern browsers, so files in the same folder may still trigger CORS errors.
The document.domain setter was once used to let subdomains talk to each other. The documentation marks this approach as deprecated, because it weakens the protection. Prefer CORS or window.postMessage when you need to communicate across origins.
Alternatives and next steps
When you need data from another origin, the normal answer is CORS. The server opts in, and the browser enforces the result. For communication between two open windows that have different origins, window.postMessage is the standard tool. It lets each side send messages and check the origin of what it receives.
To prevent a request from another site from acting on a user's behalf, a server can require an unguessable token, known as a CSRF token, in each request that changes data. Blocking embedding of a resource is another option, handled with headers such as X-Frame-Options.
The policy is one of the first security ideas a web developer meets, and it explains many confusing CORS errors. Once you know the origin rule, most cross-site browser errors become easier to read.
FAQ
Is the same-origin policy the same as CORS?
No. The same-origin policy is the default browser rule that blocks cross-origin reads. CORS is an HTTP mechanism that lets a server allow specific other origins to read its responses.
Do http and https addresses on the same domain share an origin?
No. The protocol is part of the origin, so http and https are different origins even when the host name is the same.
Can I load images or scripts from another site?
Usually yes. Embedding images, scripts, stylesheets and video from other origins is normally allowed, though a script generally cannot read the response content directly.
Why do my fetch calls fail with a CORS error?
The browser blocked your script from reading a response from a different origin. The server must send the right CORS headers, or you must call an endpoint on the same origin.
Sources
This page is based on the official documentation below. Read it for full detail.
Same-origin policy — official documentation ↗