Port numbers are used in TCP and UDP for two related reasons.
First, port numbers are used to select which service you want to reach on a given machine. If the same machine is both a mail server and a web server, it has to know somehow whether to send an incoming connection to the web server or the mail server. Typically, it does this based on the destination port number.
Second, port numbers permit more than one connection to exist between the same two addresses. If you have two machines, each with one address, without port numbers there would be no way to tell apart packets that were part of different conversations. For example, your computer could only make one connection at a time to a remote web server, and you couldn't transfer mail and web pages at the same time between the same two machines.
As for how they are used, every packet has a source address, a source port, a destination address, and a destination port. Packets that are replies have the source and destinations flipped around. Generally, the client picks a random source port and uses the appropriate port for the type of server it wants to reach on the other side. The server then replies from its service port to the port the other side picked.
A common misunderstanding is that the server assigns the connection a new port so that new connections can continue arriving on the service port. This is not true. The client picks its source port, and the connections are not confused because each connection can be identifier by the source address, source port, destination address, and destination port. (Called a 4-tuple.)
TCP and UDP are essentially the same in this respect. The main difference is that TCP is inherently bidirectional and the TCP implementation flips the source and destination around to generate replies that are part of a single connection.
With UDP, the application generates the replies and does not necessarily have to send them to the same address and port, nor are the replies part of a single connection as far as the UDP implementation is concerned.