The answer to this question can depend on the retailer you purchase from and the network provider you opt for as there is not one standard rule in place. While many retailers will allow you to transfer a number after you have placed your order some retailers will require you to do this during the purchase process and will not allow number porting after the order has been completed.
To further complicate matters some retailers may have differing rules for each network operator. They may require you to provide a PAC code during the order process when purchasing a phone on one network but allow you to transfer after your order has been completed for a phone on a different network.
For retailers that allow you to transfer your number after the order process has been completed there may still be a certain time frame in which you will be expected to do this. They may require you to transfer the number within the first couple of weeks, months or may have no time restriction in place at all.
If you want to take out a new phone contract and transfer an old number at a later date you will need to check each retailer´s specific rules regarding number porting (which you should be able to find on their website). Alternatively, if your existing contract is yet to expire you can postpone the purchase of the new contract until you have completed the obligatory period. The only other option is to request a PAC code anyway and break your existing contract prematurely, although this will most likely incur a termination fee and require you to pay the remainder of your monthly bills up front.
For more information please read Mobile Number Porting.