Your understanding concerns static compression, where the whole dataset is available when you begin compression. In a very simplified way, yes it replaces common sequences with smaller tokens to achieve compression. There are many cases where this won't even result in efficient compression (if done in a naive way), such as skewed data distribution (think "AAABBBCCC" vs. "AAAAAAABC").
More interesting is dynamic compression, which doesn't require before-hand knowledge of the full dataset. For the common CS homework algorithm, see Adaptive Huffman Coding (compared to regular Huffman Coding ) which builds the coding table as it processes the data. In addition to enabling compression of a stream, it also allows the coding table to encode data efficiently in different parts of the stream, regardless of previous data (think "AAABBBCCC").
The dynamic building of the coding table however does establish a dependency on the previous data. To correctly decode the last byte, you need to decode the first and all the following bytes.
In an unreliable environment such as the network, you don't want to resend the whole dataset if a single byte is corrupt (which would break the decoding), so there are lots of other things involved, such as chunking where you divide the payload into parts and compress them independently. A wrong checksum of the chunk would mean only that chunk is resent.
deflate
algorithm that's the core of gzip does not work that way.