There is a formula for credit scores, however its not made public due to the fact that people would manipulate it.
Get a copy of your credit report and make sure everything is correct, and there are no delinquent accounts on your history.
Don't apply for credit often as "hard pulls", or an attempt to recieve credit brings your score down.
Average age of accounts is a big factor also. If you just opened a couple accounts your average age of accounts goes down, and thats bad. Also don't close an account you have had for a long time, that can also lower your score.
Keep revolving debt(credit cards), but keep your balances down. The sweet spot seems to be as close to 33% of your available balance as possible.
Keep an auto loan. Make extra payments, but still keep the loan for a few years.
Keep a realestate account and keep it payed on time.
Thats your credit score. On top of that banks look at things like how long you have lived at a house. They want to see that you don't move around a lot. They also like to see you keep a job for as long as possible. DTI or Debt to income is also big. Make sure to keep your total monthly debt (credit cards, house, car loan) around or under 40% of your gross monthly income.
Hope that helps