I'm not sure what it has to do with a matrix - you can use every field normally and inside a matrix, there is no restriction - but I usually create a custom field type.
You can see how to create Plugins in the documentation all available fields in Craft extends the base Field class
There are function like getInputHtml where you define the html that should be displayed while you create your entry/category and the function serializeValue that defines which value should be saved into the db.
You should take a look at the default Craft fields to get used to it. Change the values you need and create your own field.
However: I think the most common approach is to look what Pixel and Tonic does and understand how it works. Then you can create custom fields via Plugin or Module
You can see how to register custom fields here. As you can see they say the same
You can refer to Craft’s own field classes for examples