Yes, it is legal, yes, it is safe- IF you do it the right way.
For ammo- yes, you can buy on line- it must be shipped to a physical address, and an adult must sign for the package.
For firearms- there are several sites that conduct firearm auctions- notably gunbroker.com, auctionarms,com, and gunsamerica.com. There are other local sites that permit you to post a gun for sale- too many to list here.
When buying a gun, the gun MUST be shipped to a FFL (dealer) in your home state if it is crossing a state line- Federal law. Your dealer that you select sends a copy of his license to the seller, who ships to that dealer. You then go see the dealer, and do the same paperwork as if buying from that dealer.
Remember that you will pay the seller for the gun, and shipping costs, and pay your dealer for his time in doing the transfer. There is no set cost for that- up to the dealer. Typically around $25 but can be more or less.
On those 3 sites you can also look at the feedback of the seller- good, bad, and neutral. If a seller has a bunch of good feedback, then you will be likely to have a good experience also.
auctionarms.com has a 3 day right of return (unfired) for any reason. You will still pay shipping both ways, but get your money back.
What we have here is... failure- to communicate.