Because total energy is conserved.
If, as an example, you consider a deuterium nucleus that is at rest, one proton, and one neutron, and compare this to a proton and neutron that are very far apart, and at rest. The proton at rest has a mass energy of 938 MeV, and the neutron has a mass energy of 939 MeV, which gives us a total energy of (roughly) 1877 MeV. Now, if, for a minute we consider the Deuterium nucleus, then the total energy contained within that nucleus has to be the total energy of the proton, the total energy of the neutron, AND the energy that it takes to stick them together. But, if we treat it as a closed system, then all of the energy that's available in the nucleus is the 1877 MeV that we had to begin with, so, the energy that we used to stick the proton and neutron together to form the nucleus of deuterium has to come out of the mass energy of the proton and the neutron.
Firstly, Protons and neutrons are composed of quarks, which come in flavours and colours. Different flavours have different charges, and different masses.
Secondly, there's three different kinds of beta decay, but the one you're thinking of involves an up quark changing to a down quark by emission of a W[sup]-[/sup] Boson, which then decays into an electron and an anti neutrino.
So the neutron decays into a proton, an electron, and an anti neutrino, because one of the particles that makes up the Neutron changes into one of the particles that makes up a proton, which has the net effect of changing the neutron into a proton, because their component particles only differ by one thing. In the process of doing so, it emits a particle, that travels a short distance before decaying into two other particles.
Proton on Wiki
Neutron on Wiki
Quark on Wiki
Beta Decay on Wiki
Q and Z bosons on Wiki