Sacrifice bunts allow a batter to use his turn at bat to move a runner, or runners, to the next base while getting out himself; essentially sacrificing his at bat for the good of the team.
In order for a batter to receive credit for a sac bunt there must be less than two outs (moving over runners is pointless if you’re the final out of the inning). If the play results in at least one runner moving up a base and the hitter being put out, the batter will be credited with the sac bunt.
If there is an error on the play, the first baseman drops a good throw for example, the batter will reach base, but he will still be credited with the sac bunt.
If the a runner advances and the batter happens to beat out the throw to first base, he will have reached base by way of a base hit—with no outs on the play, there is no sacrifice.
If a fielder attempts, for whatever reason, to throw out the runner rather than the batter at first base, the batter will make it to first base on a fielder’s choice (provided he makes it there safely).
There is one exception to this final situation (although it will rarely happen). In the event the base runner beats the attempt to put him out and the official scorekeeper judges that ordinary effort on the part of the fielders would not have been able to put out the batter at first base either the batter should receive credit for a base hit.