Building your own payment gateway is not an easy task. It is time consuming and expensive but it can be well worth it - all depending on what your intended purpose is.
Essentially, when you build a gateway - you are building the "middleman" in the transaction process. The gateway communicates directly with the front-end authorization network that is attached to the merchant's merchant account and you send and receive transaction details with that front-end per the specifications that they provide you with.
Typically, the first step is to get in touch with the integrations team at each network that you are interested in. Find out the process, cost and timetable associated with it. They will all usually require some NDA's to be signed and then will provide you with specs to code to.
During the certification process, they will run your systems through many tests and once completed, you'll be certified and added to their list of gateways.
Then you'll need to do a PCI/CISP audit. This can be expensive and will require quite a bit of infrastructure on your part as well as an on-site audit to make sure that everything is compliant.
Depending on what you intend to do with your gateway, the process may be worthwhile or it could be a major overkill. A lot of times, it is less expensive to lease, purchase or sell existing software or systems... especially with the coming price compression in the gateway side of the market.