How does Bitcoin avoid splitting into multiple p2p networks? So since Bitcoin is completely decentralized what stops multiple p2p networks from being formed?

And what happens with the block chain when two separate P2P networks eventually reach each other? Will all the nodes just agree to use the longest block chain?

Wouldn't that mean that a bunch of transactions would be lost?
As long as everyone follows the same rules, mining is convergent, because the expected interval of ten minutes allows every full node to catch up to the current state of the network.

Separate p2p networks (altcoins) derive from a different set of rules, where one of the rules is the Genesis block of the blockchain. Since their Genesis block is different and each block refers to their next ancestor until the Genesis block is reached, blocks found on a different chain are not valid for a chain with another Genesis block. They, and their ancestors are not a valid part of that chain.

As Pieter points out below,  some of the nodes' IP addresses are hard coded,  which allows new nodes to discover their first Bitcoin peers.

If the rules were the same, but two networks separately extended the chain, the heaviest chain would prevail when they reconnect. The other (lighter) chain would go stale and would not be continued. All the transactions that were previously confirmed in the network of the lighter chain would become unconfirmed and valid for mining as long as the UTXO they are spending are still unspent on the heavier chain. The process of mending a chain fork by resolving it to the benefit of the heavier chain tip is called a "chain reorganization".

Heavier and lighter here refers to the chain with the most proof of work. As long as both chain tips have the same difficulty, this would simplify to the longer chain. If the chains have different difficulties (because the separate chain tips both passed a difficulty reset), we actually need to compare the aggregated proof of work.