Is it possible to alter the source code and still have a valid Bitcoin transaction?
It depends what you change. If you don't agree with other clients on what is a valid transaction and what is a valid block, then you won't agree on who holds what bitcoins.


  How do miners know what transactions are valid if there are two different implementations of the BTC protocol?


All the people who wish to interoperate with each other have to use the same rules for what constitutes a valid transaction and what constitutes a valid block. Groups that can't agree fork the network into two networks as happened when bitcoin split into bitcoin and bitcoin cash.