How does this work exactly? Well this is a question for a cryptography course...
The important thing to remember is that cryptographic hash functions help us
maintain the integrity of data.
If we keep the hash of a bunch of data in a safe place,
we can be pretty much certain that no one can modify the data,
not even a tiny bit,
without us noticing afterwards.
We only have to hash the data again and compare the result with the hash
we kept safe.
By trusting that the hash of a given block is correct*
we can always check that all blocks up to this block
are in exactly the same state as they originally were.
Because whenever we change something,
for example the value of a transaction included in a given block,
we also change the input to our hash function,
and hence - with near certainty - also the resulting hash.
The blockchain is, in its essence, a hash chain.
Blocks for which the hashes don't match simply get rejected.
We either have no blocks, or we have blocks that weren't changed.
And therefore one could say that the blockchain is immutable**.
* This trust is earned at the consensus layer.
** We can still switch to a different chain of course,
respectively to a different fork of the blockchain.
This can also happen by accident, if something on the
consensus or network layers goes wrong.
The security of each layer in our model always depends in some way
on the security of the layers below.