Short answer: it depends. Generally, secretly purchasing a gun on someone else's behalf from a gun dealer, called a straw purchase, is illegal. If the end recipient is not disclosed to the gun dealer/seller, they cannot run the background check for the actual purchaser/owner. However, it is possible to legally purchase a gun for someone, or give a gun as a gift, but there are hoops to jump through. After all, it's a gun, and guns are very dangerous.
In most states, when giving or selling a gun, there are laws regarding registering the transfer, and some states even require the transfer be done through a licensed gun dealer. Furthermore, if you are aware, or have reason to believe, that the person receiving the gun is not legally allowed to own a gun, then it is illegal to give that person a gun. If you are planning on buying a gun for someone else, either as a favor or for a gift, you may want to take a moment to look up the laws in your state.
Illegal Gun Transfers are Serious Crimes
Under federal law, a person could face 10 years in jail as well as $250,000 in fines if they help someone obtain a gun by purchasing the gun on their behalf from a federally licensed dealer without disclosing the actual recipient. Despite the fact that this happens quite frequently, it is rarely prosecuted.
Depending on state law, transferring the ownership of a gun within your own state without registering the transfer can have very serious consequences. States like California, Colorado and New York require all gun transfers to be done through licensed dealers.
Just a couple years ago, the Supreme Court made a controversial decision on this very issue. A man who had purchased a gun for his uncle in another state did not disclose that his uncle would be the actual recipient of the firearm for the background check. When it was discovered that he lied on the application, he was arrested and convicted of making false statements under the straw purchaser law.
Despite the fact that his uncle would have passed the background check, the act of providing false information on the application justified the conviction in the Court's opinion.