There is already a question on how to write a custom extension at How to write a custom extension? , but most of the answers I read , while being good and really detailed are about magento 1, so felt the need to ask it again.
2 Answers
I believe that official documentation has a pretty good answer to this question. http://devdocs.magento.com/guides/v2.2/extension-dev-guide/bk-extension-dev-guide.html
May be you can refer this link ,which is pretty good solution for understanding for magento 2 development : M2 Custom Extention