The apiVersion tells Kubernetes what features and syntax is supported in the configuration files for the services you are working with. There are multiple supported versions, not everything is backward compatible, so by specifying the version, you can decide what and how to implement your cluster services. It is mainly for backward compatibility as Kubernetes versions move forward quickly.
How do we know what value to be used while writing yaml files? I am confused regarding the version to use while writing the yaml. kubectl explain ... describes what fields are available.