Let, Computer A will be our transmitting computer and computer B will be our receiving computer.
To start the process off, computer A sends a TCP segment to computer B with this SYN flag set. This is computer A's way of saying, "Let's establish a connection and look at my sequence number field, so we know where this conversation starts."
Computer B then responds with a TCP segment, where both the SYN and ACK flags are set. This is computer B's way of saying, "Sure, let's establish a connection and I acknowledge your sequence number."
Then computer A responds again with just the ACK flag set, which is just saying, "I acknowledge your acknowledgment. Let's start sending data."
This exchange involving segments that have SYN, SYN/ACK, and ACK sets, happens every single time a TCP connection is established anywhere. And is so famous that it has a nickname. The three-way handshake.
A handshake is a way for two devices to ensure that they're speaking
the same protocol and will be able to understand each other. Once the
three-way handshake is complete, the TCP connection is established.
Once one of the devices involved with the TCP connection is ready to close the connection, something known as a four-way handshake happens.
The computer ready to close the connection sends a FIN flag, which the other computer acknowledges with an ACK flag.
Then, if this computer is also ready to close the connection, which will almost always be the case. It will send a FIN flag.
This is again responded to by an ACK flag.