Work with good engineers, or work with good code bases off github. Theres no way you can conceive of truly amazing software yourself, unless you are linus torvalds. There are patterns, and you need to learn them externally by writing code with others or by working with exceptional open source.
Actually Linus studied the heck out of other codebases. For example, linux itself was inspired by UNIX and it is said that he studied MINIX (minified version of UNIX used for teaching) before he started developing linux.
Yes it does get written, but if you are asking how to do it, instead of proactively trying, you probably cant do it on your own. 98% of developers cant