You can follow trailhead module of how to write formula field in Salesforce:
https://trailhead.salesforce.com/content/learn/modules/point_click_business_logic/formula_fields
You can also have a look at Formula fields Limits and restriction:
https://help.salesforce.com/articleView?id=sf.formula_field_limits.htm&type=5
Now coming to your questions, You want to capture Close Time when you are closing an Opportunity, you can not do this by formula field since formula fields execute condition everytime if it meets certain criteria. You can use workflow rule to solve this problem:
Please follow below Steps:
Create a custom field of type Date/Time(Do not create formula just normal custom field).
From Setup, Process Automation-->WorkFlow Actions-->WorkFlow Rules(You can simply search for Workflow in Quickfind) & Click New Rule
In the Object drop-down list, choose Opportunity, and click Next.
In the Rule Name text box, enter relevant text.
In the Evaluate the rule when a record is: field, select:
created and everytime it's edited
In the Rule Criteria section:
Select formula and then do this:
AND( ISCHANGED(StageName), OR(TEXT(StageName) = 'Closed Won',TEXT(StageName) = 'Closed Lost'))
Click Save & Next.
Now let's create the field update action for this workflow rule.
In the Immediate Workflow Actions area, click Add Workflow Action, and select New Field Update.
In the Name text box, enter a relevant name for Field Update.
In the Field to Update drop-down list, choose the new custom date/time field that you created in Step 1.
Under Specify New Field Value, choose formula and then type this:
NOW()
Click Save.
Activate the Workflow Rule.
In order to test the functionality, Goto Opportunity Record and Change the stage to Closed Won or Close Lost, you will see the result.
Let me know if it's helpful.