When the German consonant 'b' appears either at the start or in the middle of a word, it is pronounced in a similar way to its English equivalent. In both languages, the sound is a bilabial plosive, i.e. in order to make the 'b' sound, both lips must be placed together. The airstream is then stopped as it passes through the vocal tract, causing a minor explosion when it is released.
This change in sound at the end of the word is not as surprising as it may seem. The /b/ and /p/ phonemes are both bilabial plosives and only differ in that you use your vocal cords to form the sound /b/, i.e. it is 'voiced', whereas we do not use our vocal cords to make the /p/ sound i.e. we call it 'voiceless'. To put this to the test:
Put your fingers lightly on your throat such that they rest on your voice box. Say the German word Bein (= leg) and feel how your vocal cords vibrate as you form the initial sound. Now say the German word Pein (= agony). Note that there is no vibration in your vocal cords that time.