There are two ways to connect in games:
First off, port forwarding is necessary due to routers doing Network Address Translation (or NAT) and having their own private IP range on the inside (usually 192.168.x.x)
NAT allows connections from inside to go out and replies to said connections to come back. (thus it basically does port forwarding on random port number for a while, until it times out due to inactivity)
Direct connection
You pretty much connect to person B's computer/port by adding it as 213.213.123.123: port
This would need port forwarding.
Terraria also supports this
Indirect connection.
Here you have a server online, which relays the port number you used to connect to the server, to other players.
Other players use this to connect to you and.. stuff works.
Terraria also does this for steam friends (not random people)
Also most games that have online "Lobby" to join games from, can utilize this method, you are joining said lobby after all.
There is a third way, using VPN or other tunneling application to create a tunnel through the router to the providers IP/port.
Hamachi used to be one, downside is everyone would have to use it.
VPN stuff is usually not free