A branch is just a pointer to a chain of commits. When you merge, git finds the common ancestor of both branches, compares what changed on each side since then, and combines the changes.
If both sides touched different things, you get a merge commit with two parents and history stays intact. If both sides edited the same lines, git stops and asks you: that's a conflict, and it's a feature, not a failure.