If I am denied entry to a country like Spain, am I able to buy a ticket to a country that I am certain to be able to enter (I have citizenship)? Note that the country wouldn't be the one that I came from.
If I am denied entry to a country like Spain,
That's very unlikely to happen. If you don't have proper documentation for entering Spain you will simply be denied boarding in your country of origin. Since the airlines can be fined heavily, they check VERY thoroughly.
If you arrive at a land border, they just turn you around.
am I able to buy a ticket to a country that I am certain to be able to enter (I have citizenship)?
If you are denied entry, the airline is responsible for getting you out of there. How they do this is up to them. You can offer to buy a new ticket but there are in no way obligated to accept this. In most cases, they plane you arrived in is still there and about to go back from where it came and they just will put you back on that plane. They may also try to recover the cost from you since it's your responsibility to have proper documentation for entry.
If you can't re-enter your country of origin (e.g. single use or expired Visa), the airline has to get a little more creative.