The sentences have 이다 (is) and 아니다 (is not). 이다 is special in that it directly attaches to the noun (your 선생님 이에요 is thus not correct), and this makes it change form with what comes before it. No other verb or adjective directly attaches to a noun like this.
(The gory detail: the original forms of these are actually 이어요 and 여요 which are [이(다) + 아/어/여 + 요], where 아/어/요 is the connective syllable which appears in many verb conjugations. 이어요 naturally contracts to 여요 if there's no 받침 before it. These forms are then relaxed to 이에요/예요, again to make the pronunciation easier, which means the original forms 이어요/예요 are also correct,. Similarly, the honorific form 시어요 (안녕하시어요? -> 안녕하셔요) is relaxed to 세요 (안녕하세요?))
So the conjugation of 이다 is affected by the presence of 받침 in the preceding syllable.
- X 씨는 학생이에요 / 나는 학생이야 (이에요/이야 after 받침)
- X 씨는 제 친구예요 / 쟤는 내 친구야 (예요/야 if no 받침)
아니에요 is not affected by it because it comes after a space. But it can shorten to 아녜요 (or the less common 아녀요), independent of the presence of 받침.
- X 씨는 학생이 아니에요 / X 씨는 제 친구가 아니에요. (particle 이/가 is the part that changes depending on the 받침)
The particle 이/가 may be omitted, but their presence or omission does't affect the 아니에요 form.
Note that 에요 in 아니에요 is unrelated to the 이다 forms in 학생이에요 or 친구예요. This 이에요, 예요, and (아니)에요 distinction is something a lot of people (including a good percentage of native speakers) get wrong because they are indistinguishable when spoken.
- 뭐예요? (O) / 뭐에요? (X) / 이거예요 (O) / 이거에요 (X) / 여기예요 (O) / 여기에요 (X)
- 무슨 일이에요 (O) / 무슨 일이예요 (X) / 무슨 일예요 (X) / 무슨 일에요 (X).