It depends on your nationality. EU/EEA citizens can move freely. Non-EU nationals need a visa — the most popular options are the Non-Lucrative Visa, Digital Nomad Visa, and the Work Visa.
Yes — the NIE (Número de Identificación de Extranjero) is your foreigner identification number. You cannot open a bank account, sign a lease, buy a car, or work legally without it. It's one of your first priorities.
After living in Spain legally for 5 continuous years, you can apply for long-term residency. Spanish citizenship requires 10 years (with some exceptions for Latin Americans, who only need 2).