By not using any extensions that your compiler provides and by not relying on undefined behavior (even if it seems to work with your compiler) and not relying on implementation-defined behavior. When in doubt, you can consult the C++ standard: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf
But especially if you don't much about C++ yet, the easiest way to make sure the code you write is portable is to test it with other compilers occasionally.